Skip to main content

Using a pointer

Using a pointer 

Following example shows how a pointer can be used to access the value in a variable. 
int n=20,x; n x 20 garbage 
1002 2000 
int *ptr_n; /*uninitialized pointer*/ ptr_n garbage 
1006 
ptr_n=&n; /* store address of n in ptr_n */ ptr_n 
1002 
1006 
*ptr _n = 30; /*modify value */ n 
30
1002 
Here we have accessed the contents of variable n using the pointer ptr_n. This can be done using the *operator. 
*(ptr_n) => value at(ptr_n) => value at 1002 => 30  

Comments