star opeerator in c++ code example

Example 1: c++ dereference a pointer

int number;
int value;
int * pNumber;

number = 5;
pNumber = &number;

assert(pNumber); // check before dereferenceing to prevent errors
value = *pNumber // Use * to dereference the pointer

Example 2: c++ pointers

// my first pointer
#include <iostream>
using namespace std;

int main ()
{
  int firstvalue, secondvalue;
  int * mypointer; //creates pointer variable of type int

  mypointer = &firstvalue;
  *mypointer = 10;
  mypointer = &secondvalue;
  *mypointer = 20;
  cout << "firstvalue is " << firstvalue << '\n';   //firstvalue is 10
  cout << "secondvalue is " << secondvalue << '\n'; //secondvalue is 20
  return 0;
}

Tags:

Misc Example