Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Pointers in C

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

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

Array of pointers to character


 Array of pointers to character to store a list of strings.
 EX-
#include <stdio.h>
const int MAX = 5;
int main ()
{
char *names[] = {
"RRSDCE",
"CSE",
"CIVIL",
"EEE"
"MECH"
};
int i = 0;
for ( i = 0; i < MAX; i++)
{
printf("Value of names[%d] = %s\n", i, names[i] );
}

return 0;
}

Output of above program

Value of names[0] = RRSDCE


Value of names[1] = CSE
Value of names[2] = CIVIL
Value of names[3] = MECH
Pointer to Pointer
 A pointer to a pointer is a form of multiple indirection, or a chain of pointers.
 Normally, a pointer contains the address of a variable. When we define a pointer to a
pointer, the first pointer contains the address of the second pointer, which points to the
location that contains the actual value as shown below.

 Deceleration of pointer to pointer


 int **var;
 EX-
#include <stdio.h>
int main ()
{
int var;
int *ptr;
int **pptr;
var = 10;
/* take the address of var */
ptr = &var;
/* take the address of ptr using address of operator & */
pptr = &ptr;
/* take the value using pptr */
printf("Value of var = %d\n", var );
printf("Value available at *ptr = %d\n", *ptr );
printf("Value available at **pptr = %d\n", **pptr);
return 0;
}

Output of Above Program

Value of var = 10
Value available at *ptr = 10
Value available at **pptr = 10

You might also like