Programming and Graphics

(Kiana) #1

128 Introduction to C++ Programming and Graphics


float b=1.2;
double c=3.45;
char d=99;

cout << setw(5) << a <<""<<(unsigned int) &a << endl;
cout << setw(5) << b <<""<<(unsigned int) &b << endl;
cout << setw(5) << c <<""<<(unsigned int) &c << endl;
cout << setw(5) << d <<""<<(unsigned int) &d << endl;

return 0;
}

The output of the code is:


4 3219066260
1.2 3219066256
3.45 3219066248
c 3219066247

The memory addresses printed as unsigned integers appear in the second col-
umn.


We can store the memory address of a variable in a new integer variable.
In our example, we can state:


unsigned int mab = (unsigned int) &b;

The parentheses on the right-hand side implement typecasting. When printed,
the integermabwill have the value 3219066256.


Pointer variables


Instead of implementing typecasting, we can store the memory address
of a variable in another variable of the pointer type called, for example,pname.
This practice prevents us from confusing true integer variables with those hold-
ing memory addresses.


A pointer corresponding to an integer variable is declared as:

int * pname

or


int *pname
A pointer corresponding to a real variable registered in double precision
is declared as:


double * pname
Free download pdf