Detailed Print – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  


Shop
siteicon
Code Learning#CPP#7503

Simple pointer declaration

Understanding how pointer variable is a notation for address of an entity in memory.

Learning Objectives

  • Understanding declaration of a pointer type variable.
  • Understanding use of address of (&) operator.
  • Understanding use of * as de-referencing for reading value of a pointer variable.

Source Code

		 

Click to open popup

Run Output

0x28ff2c
10

Note: Line 1 above can be different in different systems and different occasions
is it denotes memory address.

Code Understanding

int a=10;
This is normal way of variable declaration

int *p=&a;
Here we have declared p with an asterisk which denotes that it contains an address of memory place which is meant to store an integer value. We assign address of a (&a) to p the pointer type variable.

cout<<p<<endl;
Since p is pointer variable so it will contain memory address. The address value show here will be different for different run occasion and different systems as it would be allocated only when the new pointer variable is declared.

cout<<*p<<endl;
This will print 10 contain in the pointer variable p. *p denotes the value at pointer variable p. Remember that at the time of declaration * denotes the type of variable and at the time use it denotes the value at given location.

Notes


Suggested Filename(s): ptr.cpp, simp-ptr.cpp