Monday, August 13, 2012

Pointer In C (In Depth) Part 1



POINTER

Pointer is a variable just like other variables of c but only difference is unlike the other variable it stores the memory address of any other variables of c. This variable may be type of int, char, array, structure, function or any other pointers. 

For examples:

(1) Pointer p which is storing memory address of a int type variable:
int i=50;
int *p=&i;

(2) Pointer p which is storing memory address of an array:

int arr[20];
int (*p)[20]=&arr;



(3) Pointer p which is storing memory address of a function:

char display(void);
char(*p)(void)=&display;

(4) Pointer p which is storing memory address of struct type variable:

struct abc{
int a;
float b;
}var;
struct abc *p=&var;

Pointer is a user defined data type which creates special types of variables which can hold the address of primitive data type like charintfloatdouble or user defined data type like function, pointer etc. or derived data type like array, structure, union,enum.

Examples:
int *ptr;
int (*ptr)();
int (*ptr)[2];

In c programming every variable keeps two type of value.
  1. Contain of variable or value of variable.
  2. Address of variable where it has stored in the memory.
Meaning of following simple pointer declaration and definition:
int a=5;
int * ptr;
ptr=&a;

Explanation:

About variable a:
  1. Name of variable : a
  2. Value of variable which it keeps: 5
  3. Address where it has stored in memory : 1025 (assume)
About variable ptr:
  1. Name of variable : ptr
  2. Value of variable which it keeps: 1025
  3. Address where it has stored in memory : 5000 (assume)


Note: A variable where it will be stored in memory is decided by operating system. We cannot guess at which location a particular variable will be stored in memory.

Meaning of following pointer declaration and definition
int a=50;
int *ptr1;
int **ptr2;
ptr1=&a;
ptr2=&pt1;

Explanation:
About variable a:
  1. Name of variable : a
  2. Value of variable which it keeps: 50
  3. Address where it has stored in memory : 5000 (assume)
About variable ptr1:
  1. Name of variable : ptr1
  2. Value of variable which it keeps: 5000
  3. Address where it has stored in memory : 9000 (assume)
About variable ptr2:
  1. Name of variable : ptr2
  2. Value of variable which it keeps: 9000
  3. Address where it has stored in memory : 9555 (assume)

Note:  * is know as indirection operator which gives content of any variable.
           & is know as reference operator which gives address where variable has stored in memory.

Cancellation rule of above two operators:

* and & operators always cancel to each other. i.e.
*&p=p
But it is not right to write:
&*p=p


Example:

What will be output of following c program?

void main()
{
       int x=25;
       int *ptr=&x; //statement one
       int **temp=&ptr; //statement two
       printf(“%d %d %d”.x.*ptr,**temp);
}

Output: 25 25 25

Explanation:
As we know value of variable x is 25.
*ptr= *(&x) //from statement one
=*&x
=x //using cancellation rule
=25
**temp= **(&ptr)=*(*&ptr)=*ptr=*(&x)=*&x=x=25


Read complex pointers in C Programming

Rule 1. 

Assign the priority to the pointer declaration considering precedence and associative according to following table.




(): This operator behaves as bracket operator or function operator.

[]: This operator behaves as array subscription operator.

*: This operator behaves as pointer operator not as multiplication operator.

Identifier: It is not an operator but it is name of pointer variable. You will always find the first priority will be assigned to the name of pointer.
Data type: It is also not an operator. Data types also includes modifier (like signed int, long double etc.)

Read following pointer

char (* ptr)[3]

Step 1: () and [] enjoys equal precedence. So rule of associative will decide the priority. Its associative is left to right So first priority goes to ().
                                                             
Step 2: Inside the bracket * and ptr enjoy equal precedence. From rule of associative (right to left) first priority goes to ptr and second priority goes to *.
                                                              
Step3: Assign third priority to [].
                                                             
Step4: Since data type enjoys least priority so assign fourth priority to char.
                                                              
ptr is pointer to such one dimensional array of size three which content char type data. 

Read following pointer

float (* ptr)(int)

Assign the priority considering precedence and associative.
                                                              
ptr is pointer to such function whose parameter is int type data and return type is float type data.

Rule 2: 

Assign the priority of each function parameter separately and read it also separately.

Understand it through following example.

Read following pointer

void (*ptr)(int (*)[2],int (*) void))

Assign the priority considering rule of precedence and associative.

Now read it following manner:
 
ptr is pointer to such function which first parameter is pointer to one dimensional array of size two which content int type data and second parameter is pointer to such function which parameter is void and return type is int data type and return type is void

Read following pointer

int ( * ( * ptr ) [ 5 ] ) ( )

Assign the priority considering rule of precedence and associative.
                                                   
ptr is pointer to such array of size five which content arepointer to such function which parameter is void and return type is int type data.

Read following pointer

double*(*(*ptr)(int))(double **,char c)
                                                      
ptr is pointer to function which parameter is int type data and return type is pointer to function which first parameter is pointer to pointer of double data type and second parameter is char type data type and return type is pointerto double data type.


Read following pointer
 
unsigned **(*(*ptr)[8](char const *, ...)

Assign the priority considering rule of precedence and associative.
                                                 
ptr is pointer to array of size eight and content of array is pointer to function which first parameter is pointer to character constant and second parameter is variable number of arguments and return type is pointer to pointer ofunsigned int data type.