Saturday, 7 December 2013

Pointer

POINTERS


&------->  ADDRESS OF
*---- ----> VALUE OF ADDRESS,
                              (OBJECT AT THAT LOCATION,)
                          (INDIRECTION OPERATOR), 
     (DEREFERENCE)

(ABOVE BOTH ARE UNARY OPERATOR AND 
THEIR ASSOCIATIVITY IS FROM RIGHT TO LEFT) 
think about it:
                    main()
{
int a=10;
printf("value of a: %d \n",a);                     //10
 printf("address of a: %u \n",&a);            //65494
printf("address of a: %p \n",&a);            //FFD6
printf("address of a: %x \n",&a);           //ffd6

/* printf("value of a : %d",*a);  //it is error. since 'a' is a value variable not an address variable.we can use * only for address(indirectly or directly) */  
printf("value of a :%d",*&a); //correct      //10
}

Definition of Pointer :
                              A pointer is a variable of a derived data type which can hold the address of ordinary variable, array variable, pointer variable, user define data type variable.
  
                   It can also hold the address of a function.

Declaration of a pointer variable:
                       data type *variable name = < initialization >;
ex:
      int *s;      //  s variable of integer pointer
      float *k;
      char *p; 

" the way of reading , s is a variable of type integer pointer."  

NOTE:
             int *ptr;
  1. ptr is a variable of type integer pointer(*);
  2. *ptr gives integer.
  3. In ptr we can store address of integer variable;
  4. ptr is call, point the variable of type integer or integer pointer or pointer to integer.
  5. this operator (*) gives the value at the address, pointed by point. 
Example:
                 main()
{
int i=10;
int *s;
s=&i;
printf("value of i=%d",i);
printf("address of i=%d",&i);
printf("value of i=%d",*&i);
printf("value of s=%u",s);
printf("address of s=%u",&s);
printf("value of i=%d\n",*s);
printf("value of s=%u\n",*&s) ;
}

*s is replaced with object (i) at that location.
 

No comments: