Programming and Graphics

(Kiana) #1

5.1 Pointers to scalars and characters 133


{
double a=8.45;
double * memada;
double ** memadb;

memada = &a;
memadb = &memada;

double verifya = *memada;
double verifyb = **memadb;

cout << a << endl;
cout << memada <<""<<memadb << endl;
cout << verifya <<""<<verifyb << endl;
return 0;

}

Running the code prints on the screen:


8.45
0xbfd40150 0xbfd4014c
8.45 8.45

Third- and high-order pointers are defined in similar ways.


Inverse typecasting


At the beginning of this section, we saw that the memory address of
a variable can be stored as a regular integer of a non-pointer type through
typecasting.


Double use of the dereference operator allows us to map the integer back
into the variable. To demonstrate the method, we consider the statements:


float b=1.2;
unsigned int mab = (unsigned int) &b;
cout << b << " " << mab <<""<<*(float*) mab << endl;

The output on the screen is:


1.2 3215432204 1.2

The expression (float) mabtypecasts the integermabas a pointer corre-
sponding to a float, and then extracts the pointer content.

Free download pdf