Header Ads

Pointer to pointer - Objective C

A pointer to a pointer is a form of multiple indirection or a chain of pointers. Normally a pointer contain 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 variable that is pointer to a pointer must be declared as such. This is done by placing an additional asterisk  in front of its name. For example following is the declaration to declare a pointer to a pointer of type int:

int **var;

When a target value is indirectly pointed by a pointer to pointer, accessing that value requires that the asterisk operator be applied twice, as is shown below in the example.

#import <Foundation/Foundation.h>
int main()
{

      int var = 3000;
      int *ptr, **pptr;
           
         //take the address of var
       ptr = &var;

           //take the address of ptr using address of operator &   
       pptr = &ptr;

             //tkae the value using pptr
       NSLog(@"value of var = %d\n", var);
       NSLog(@"value available at *ptr  = %d\n", *ptr);
       NSLog(@"value available at **pptr = %d\n", **pptr);
     
      return 0;
}




output is soon in video tutorial


No comments:

Powered by Blogger.