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;
int *ptr;
- ptr is a variable of type integer pointer(*);
 - *ptr gives integer.
 - In ptr we can store address of integer variable;
 - ptr is call, point the variable of type integer or integer pointer or pointer to integer.
 - 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.
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:
Post a Comment