Pointer is a variable that stores the address of another variable.
Consider the following statement −
int qty = 179;
In memory, the variable can be represented as follows −
It means ‘p’ is a pointer variable, which holds the address of another integer variable, as shown below −
Int *p;
Address operator (&) is used to initialise a pointer variable.
For Example − int qty = 175;
int *p;
p= &qty;
It is collection of addresses (or) collection of pointers
Following is the declaration for array of pointers −
datatype *pointername [size];
For example,
int *p[5];
It represents an array of pointers that can hold 5 integer element addresses.
‘&’ is used for initialisation.
For Example,
int a[3] = {10,20,30}; int *p[3], i; for (i=0; i<3; i++) (or) for (i=0; i<3,i++) p[i] = &a[i]; p[i] = a+i;
Indirection operator (*) is used for accessing.
For Example,
for (i=0, i<3; i++) printf ("%d", *p[i]);
Given below is the program for array of pointers −
#include<stdio.h> main ( ){ int a[3] = {10,20,30}; int *p[3],i; for (i=0; i<3; i++) p[i] = &a[i]; printf ("elements of the array are \n"); for (i=0; i<3; i++) printf ("%d \t", *p[i]); }
When the above program is executed, it produces the following result −
elements at the array are : 10 20 30
Given below is the program for the array of pointers to strings −
#include <stdio.h> #include <stdlib.h> int main(void){ char *a[5] = {"one", "two", "three", "four", "five"}; int i; printf ( "the strings are at locations:\n"); for (i=0; i<5; i++) printf ("%d\n", a[i]); return 0; }
When the above program is executed, it produces the following result −
The strings are at locations: 4210688 4210692 4210696 4210702 4210707