NAME:- DARJI MIT BIPINBHAI
TOPICK : - POINTER TO POINTER
POINTER TO ARRAY
ARRAY TO POINTER
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.
 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.
.
−
I ptr1
ptr2
 A variable that is a pointer to a pointer must be
declared as such. This is done by placing an additional
asterisk in front of its name. For example, the following
declaration declares a pointer to a pointer of type int −
int **var
When a target value is indirectly pointed to by a pointer to a
pointer, accessing that value requires that the asterisk
operator be applied twice, as is show below in the example-;
#include <stdio.h>
Int main (){
int var ;
int*ptr;
int**pptr;
var=3003
/*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 %dn”,var);
print f (“value available at *ptr =%dn,*ptr);
print f(“value available at **pptr =%dn”,**pptr);
return 𝜃;
}
When the above code is compiled and executes , it produces the
following result
 When an array is declared, the compiler allocates a base
address and sufficient amount of storage is the location
of the first element(index 0) of the array. The compiler also
defines the array name as a constant pointer to the first
element. Suppose we declare an array x as follows:
int X[5]={1,2,3,4,5};
 Suppose the base address of x is 1000 and assuming
that each integer requires two bytes, the five elements
will be stored as follows:
Elements X[0] X[1] X[2] X[3]
X[4]
value
Address
Base address
the name X is defined as a constant pointer pointing to the first element,
X[0] and therefore the value of X is 1000, the location where X[0] is stored. This
is,
X= &x [0] =1000
1 1000 2 1002 3 1004 4 1008
 if we declare p as an integer pointer, then we can
make the pointer p to point to the array x by the
following assignment:
p=x;
This is equivalent to
p=&x[0];
 Now, we can access every value of X using p++ to
move from one element to another. The relationship
between p and X is shown as :
p=&x[0](=1000)
p+1=&x[1](=1002)
p+2=&x[2](=1004)
p+3=&x[3](=1006)
p+4=&x[4](=1008)
 You may notice that the address of an
element is calculated using its index and the
scale factor of the data type. For instance,
address of x[3] = base address+(3x scale factor of int)
=1000+(3×2) 1006
 When handing array, instead of using array
indexing, we can pointers to access array elements.
Note that *(p+3) gives the value of x[3]. The pointer
accessing method is much faster than array indexing.
 One important use of pointer is in handling of a
table of strings. Consider the following array of
strings:
char name [3] [25];
 This says that the name is a table containing three
names, each with a maximum length of 25
characters (including null character). The total
storage requirements for the name table are 75
bytes.
 We know that rarely the individual strings will be of
equal lengths. Therefore, instead of making each row
a fixed number of characters, we can make it a
pointer to a string of varying length.
For example,
char *name[3] = {
“New Zeland”,
Australia”,
“India”
};
declares name to be an array of three pointers to
characters, each pointer pointing to a particular name as:
name[0] New zeland
name[1] Australia
name[2] India
This declaration allocates only 28 bytes, sufficient to hold
all the characters as shown
The following statement would print out all the three
names;
for(i=0; i<=2; i++)
printf(“%sn “, name[i]);
 To access the jth character in the ith name, we may
write as
*(name[i]+j)
 The character array with the rows of varying length
are called ragged array’ and are better handled by
pointers.
 Remember the difference between the notations *p[3]
and (*p)[3].Since *has a lower precedence than [],*p[3]
declares p as an array of 3 pointers while (*p)[3]
declares p as a pointer to an array of three elements.
N e w Z e a l a n d 0
A u s t r a l i a 0
I n d i a 0
Pointer in c

Pointer in c

  • 1.
    NAME:- DARJI MITBIPINBHAI TOPICK : - POINTER TO POINTER POINTER TO ARRAY ARRAY TO POINTER
  • 2.
    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.  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.
  • 3.
  • 4.
     A variablethat is a pointer to a pointer must be declared as such. This is done by placing an additional asterisk in front of its name. For example, the following declaration declares a pointer to a pointer of type int − int **var When a target value is indirectly pointed to by a pointer to a pointer, accessing that value requires that the asterisk operator be applied twice, as is show below in the example-;
  • 5.
    #include <stdio.h> Int main(){ int var ; int*ptr; int**pptr; var=3003 /*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 %dn”,var); print f (“value available at *ptr =%dn,*ptr); print f(“value available at **pptr =%dn”,**pptr); return 𝜃; } When the above code is compiled and executes , it produces the following result
  • 6.
     When anarray is declared, the compiler allocates a base address and sufficient amount of storage is the location of the first element(index 0) of the array. The compiler also defines the array name as a constant pointer to the first element. Suppose we declare an array x as follows: int X[5]={1,2,3,4,5};  Suppose the base address of x is 1000 and assuming that each integer requires two bytes, the five elements will be stored as follows: Elements X[0] X[1] X[2] X[3] X[4] value Address Base address the name X is defined as a constant pointer pointing to the first element, X[0] and therefore the value of X is 1000, the location where X[0] is stored. This is, X= &x [0] =1000 1 1000 2 1002 3 1004 4 1008
  • 7.
     if wedeclare p as an integer pointer, then we can make the pointer p to point to the array x by the following assignment: p=x; This is equivalent to p=&x[0];  Now, we can access every value of X using p++ to move from one element to another. The relationship between p and X is shown as : p=&x[0](=1000) p+1=&x[1](=1002) p+2=&x[2](=1004) p+3=&x[3](=1006) p+4=&x[4](=1008)
  • 8.
     You maynotice that the address of an element is calculated using its index and the scale factor of the data type. For instance, address of x[3] = base address+(3x scale factor of int) =1000+(3×2) 1006  When handing array, instead of using array indexing, we can pointers to access array elements. Note that *(p+3) gives the value of x[3]. The pointer accessing method is much faster than array indexing.
  • 9.
     One importantuse of pointer is in handling of a table of strings. Consider the following array of strings: char name [3] [25];  This says that the name is a table containing three names, each with a maximum length of 25 characters (including null character). The total storage requirements for the name table are 75 bytes.  We know that rarely the individual strings will be of equal lengths. Therefore, instead of making each row a fixed number of characters, we can make it a pointer to a string of varying length.
  • 10.
    For example, char *name[3]= { “New Zeland”, Australia”, “India” }; declares name to be an array of three pointers to characters, each pointer pointing to a particular name as: name[0] New zeland name[1] Australia name[2] India This declaration allocates only 28 bytes, sufficient to hold all the characters as shown
  • 11.
    The following statementwould print out all the three names; for(i=0; i<=2; i++) printf(“%sn “, name[i]);  To access the jth character in the ith name, we may write as *(name[i]+j)  The character array with the rows of varying length are called ragged array’ and are better handled by pointers.  Remember the difference between the notations *p[3] and (*p)[3].Since *has a lower precedence than [],*p[3] declares p as an array of 3 pointers while (*p)[3] declares p as a pointer to an array of three elements. N e w Z e a l a n d 0 A u s t r a l i a 0 I n d i a 0