Coupon Accepted Successfully!


Pointer Declaration

Pointer variables can be declared as follows:


int *x;

float *f;

char *y;

  1. In the first statement, ‘x’ is an integer pointer, and it informs the compiler that it holds the address of any integer variable. In the same way, ‘f’ is a float pointer that stores the address of any float variable, and ‘y’ is a character pointer which stores the address of any character variable.
  2. The indirection operator (*) is also called the dereference operator. When a pointer is dereferenced, the value at that address stored by the pointer is retrieved.
  3. Normal variables provide direct access to their own values; whereas a pointer indirectly accesses the value of a variable to which it points.
  4. The indirection operator (*) is used in two distinct ways with pointers: declaration and dereference.
  5. When a pointer is declared, the star indicates that it is a pointer, not a normal variable.
  6. When the pointer is dereferenced, the indirection operator indicates that the value at that memory location stored in the pointer is to be accessed rather than the address itself.
  7. It should also be noted that * is the same operator which can be used as the multiplication operator. The compiler knows which operator to call, based on the context.
  8. The ‘&’ is the address operator, and it represents the address of the variable. The address of any variable is a whole number. The operator ‘&’ immediately preceding the variable returns the address of the variable. In the below given example, ‘&’ immediately precedes the variable ‘num,’ which provides the address of the variable.

13.1 Write a program to display the address of the variable.





int n;

cout<<“Enter a Number = ”;
cout<<“Value of n = ”<<n;
cout<<“Address of n= ” <<(unsigned)&n;


Enter a Number = 10
Value of n = 10
Address of n=4068

 The memory location of a variable is system dependent. Hence, the address of a variable cannot be predicted immediately. In the above example, the address of the variable ‘n’ that is observed is 4068. In Figure, three blocks are shown to be related to the above program. The first block contains the variable name. The second block represents the value of the variable. The third block is the address of the variable ‘n,’ where 10 is stored. Here, 4068 is the memory address. The address of the variable depends on various things; for instance, memory model, addressing scheme, and present system settings.
Fig: Variable and its memory address

13.2 Write a program to declare a pointer. Display the value and address of the variable-using pointer.





void main()


int *p;

int x=10;



printf (“\nx=%d&x=%u\t(Using printf())”,x,p);

cout<<“\n x=”<<x <<“ &x=”<< &x <<“(Using cout())”;

printf (“\nx=%d&x=%u\t(Using pointer)”,*p,p);

cout<<“\n *p=”<<*p <<“\t&p=” <<p <<“\t(Contents of



x=10 &x = 65524 (Using printf())

x=10 &x = 0x8f94fff4 (Using cout())

x=10 &x = 65524 (Using pointer)

*p=10 &p = 0x8f94fff4 (Contents of pointer)


Explanation: In the above program, *p is an integer pointer and x is an integer variable. The address of variable x is assigned to pointer pAmpersand (&) operator preceded with a variable displays the memory location of that variable. The printf() and cout() statements display addresses in different formats. The printf() statement displays the address as an unsigned integer, whereas the cout() statement displays the address in a hexa-decimal format. We can access the contents of variable x using pointer p. The output of the program is as shown above.


13.3 Write a program to display memory address of a variable. Type cast the memory address from hexadecimal to unsigned integer.



void main()


int a;

float b;

char c;


cout<<“\n Enter integer number = ”;


cout<<“\n Enter float number = ”;


cout<<“\n Enter character = ”;


cout<<“\nThe entered int is = ”<<a;

cout<<“\nThe entered float is = ”<<b;

cout<<“\nThe entered char is = ”<<c;

cout<<“\n\nThe entered number is stored at location = ” <<(unsigned)&a;

cout<<“\n The entered float is stored at location= ”<<(unsigned)&b;

cout<<“\n The entered char is stored at location = ”<<(unsigned)&c;




Enter integer number = 34

Enter float number = 343.34

Enter character = g

The entered int is = 34

The entered float is = 343.339996

The entered char is = g

The entered number is stored at location = 4096

The entered float is stored at location = 4092

The entered char is stored at location = 4091


Explanation: In the above program, variables a, b, and c of intfloat, and char type are declared. The entered values are displayed along with their addresses. The addresses of the variables are displayed by a preceding ampersand (&) operator with a variable name. The type-casting syntax (unsigned) is done to type cast the hexadecimal address into an unsigned integer.

Here, you can observe the addresses in descending order. This is because all automatic variables are stored in a stack. The stack always starts from top to lower memory addresses.

Test Your Skills Now!
Take a Quiz now
Reviewer Name