Data Structure Pointer

Pointer

To point the address of the value stored anywhere in the computer memory, a pointer is used. Dereferencing the pointer is to obtain the value stored at the location. The performance for a repetitive process is improved by the pointer. Such processes include:

  • Traversing String
  • Lookup Tables
  • Control Tables
  • Tree Structures

Pointer Details:

  • Pointer arithmetic: In pointers, four arithmetic operators can be used: ++, –, +, -.
  • The array of pointers: An array can be defined to hold several pointers.
  • Pointer to pointer: We can have a pointer on a pointer and so on in C.
  • Passing pointers to functions in C: To enable the passed argument to be changed in the calling function by the called function, we can pass an argument by reference or by address.
  • Return pointer from functions in C: In C, a function can also return a pointer to the local variable, static variable, and dynamically allocated memory.

Pointer Example:

#include <stdio.h>  
 
int main( )  
{  
int x = 10;  
int *y;  
y = &x;  
 
printf ("value of x = %d\n", x);  
printf ("value of x = %d\n", *(&x));  
printf ("value of x = %d\n", *y);  
printf ("address of x = %u\n", &x);  
printf ("address of x = %d\n", y);  
printf ("address of y = %u\n", &y);  
printf ("value of y = address of x = %u", y);  
return 0;  
}

Output:

Pointer to Pointer Example:

#include <stdio.h>  
 
int main( )  
{  
int x = 10;
int *y;  
int **z;  
y = &x;  
z = &y;  
printf ("value of x = %d\n", x);  
printf ("value of x = %d\n", *(&x));  
printf ("value of x = %d\n", *y);  
printf ("value of x = %d\n", **z);  
printf ("value of y = address of x = %u\n", y);  
printf ("value of z = address of y = %u\n", z);  
printf ("address of x = %u\n", &x);  
printf ("address of x = %u\n", y);  
printf ("address of x = %u\n", *z);  
printf ("address of y = %u\n", &y);  
printf ("address of y = %u\n", z);  
printf ("address of z = %u\n", &z);  
return 0;  
}

Output: