Understanding Pointers in C/C++ | C Programming and Computer Geeks

Search MY Blog

Sunday, 1 December 2013

Understanding Pointers in C/C++

When we declare a variable in C/C++ etc.. like int x or int y , it tells the compiler to reserve the space of two bytes(Size depends on OS Platforms/Compilers ) in the memory to store the integer type value and identify the storage location in the memory by 'x' or 'y' etc.
So every variable has three properties 1. Identifier (Name like x or y) 2. Value(x=10 or y=15 etc.) 3. Its address (hexadecimal) in the Temporary Memory i.e. RAM. Since compiler makes our job simpler by giving an opportunity to know only the variable’s name and its value then in normal situation we have no need to worry about the location or address of the variable. But in advanced programming and certain types of problems we have to handle and use the address of variables. So the concept of Pointers solves this problem. 
We can say Pointer Definition as "Pointers are those variables which holds the address of another variable”. In C/C++ we can see the “Address” or “Memory Location Number” of any variable by using "&" i.e. if we declare a variable 'x' eg: int x; we can see its address by printf statement as follows printf("\n %d", &x);
Now to handle the address of variables we have the facility to declare the Pointer variable like this int *p; this statement tells the compilers to reserve the space in memory to store the address of integer type variable. To assign the address of any integer type variable to 'p' we have to write the statement like p = &x; 

Example Program:

Understanding Pointers in C
Pointers in C



void main()
{
 int x = 34; //variable declaration and definition.
 int *p; //pointer declaration.

 /* p is a pointer which points to a integer variable or ‘p’   is  a pointer which holds the address of an integer variable..  */
 p = &x; //Assigning the address of variable x to pointer ‘p’.
 printf("Address of variable x is : %x",p);//prints the  address  of variable x. here %x displays Address in Hexa  decimal format.
}

Clickhere for more C Tutorials and Concepts.



Search This Blog