Pointers in C
Pointers in C
Pointers in C
The pointer in C language is a variable which stores the address of another variable.
Like any variable or constant, you must declare a pointer before using it to store any
variable address.
The general form of a pointer variable declaration is –
Data_type * Variable_Name;
Here, type is the pointer's base type.
It must be a valid C data type and var-name is the name of the pointer variable.
The asterisk * used to declare a pointer is the same asterisk used for multiplication.
However, in this statement the asterisk is being used to designate a variable as a
pointer.
Pointer Deceleration
int *p; /* pointer to an integer */
double *p; /* pointer to a double */
float *p; /* pointer to a float */
char *ch /* pointer to a character */
Array of pointer
Array of pointers is an indexed set of variables, where the variables
are pointers (referencing a location in memory).
Array of pointers is allows us to numerically index a large set of variables.
EX-
#include <stdio.h>
const int ARRAY_SIZE = 5;
int main ()
{
/* first, declare and set an array of five integers: */
int array_of_integers[] = {5, 10, 20, 40, 80};
/* next, declare an array of five pointers-to-integers: */
int i, *array_of_pointers[ARRAY_SIZE];
for ( i = 0; i < ARRAY_SIZE; i++)
{
/* for indices 1 through 5, set a pointer to
point to a corresponding integer: */
array_of_pointers[i] = &array_of_integers[i];
}
for ( i = 0; i < ARRAY_SIZE; i++)
{
/* print the values of the integers pointed to by the pointers: */
printf("array_of_integers[%d] = %d\n", i, *array_of_pointers[i] );
}
return 0;
}
Output of above Program
array_of_integers[0] = 5
array_of_integers[1] = 10
array_of_integers[2] = 20
array_of_integers[3] = 40
array_of_integers[4] = 80
return 0;
}
Value of var = 10
Value available at *ptr = 10
Value available at **pptr = 10