Advanced pointers

Let us look at some of the declarations.


int *ptr1; /* fine. We all know it is a pointer*/
int **ptr2;/*ptr2 is a pointer to pointer*/
int *ptr3[10];/*array of pointers*/
int (*ptr4)[10];
int (*ptr5)(int); 

OK. ptr4 is a pointer to an array and ptr5 is the dreaded function pointer.

Pointer to an array

We know that array in any expression becomes a pointer. So then why do we need a pointer to an array?

int arr[5];
int m = *(arr+3);

Here we are using array name as a pointer, doing some arithmetic with it and dereferencing it. But arr in this case, is like an integer pointer. But if we take address of arr, that becomes pointer to an array.

int (*ptr)[5];
ptr = &arr;

ptr points to the entire array, not to one element. So if we dereference it, we get the entire array. To access say 2 element of array using ptr, we need subscript operator also.

(*ptr)[2] = 17;

And if we increment ptr here, it gets moved by 5 integers.

Where is this pointer to array is useful? In functions. When a 2D array is passed to function, it gets converted to pointer to one D array.

int arr[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};  
print_array(arr);   
void print_array(int (*arr)[4])  
{  
    int i,j;  
    for(i=0;i<3;i++,arr++)  
        for(j=0;j<4;j++)  
           printf("%d",(*arr)[j]);  
}  

Next let us look at another complex pointer. int (*ptr)(int). This is a function Pointer.

Function Pointer

Function pointer holds the address of a function.  If we want to write a function pointer for the function of syntax

  void foo(int,int),

the pointer declaration should be,

void (*foo_pointer)(int,int);

It is read as - foo_pointer is a pointer to function which takes 2 int parameter and does not return anything. 

To assign a value to this function pointer, you should use

foo_pointer = foo; /* or &foo*/

Let us write some program here


 #include<stdio.h>  
 int fun();  
 int main()  
 {  
   int (*fptr)();  
   fptr = fun;  
   fptr();  
   return 0;  
 }  
 int fun()  
 {  
   printf("Hello world\n");  
   return 1;  
 }  

fptr in this program is a funciton pointer to a function with no parameters and returning an int.  fptr  is assigned to the function fun. In the next line the function is called using fptr.

Array of pointers

Many a times a set of string is stored using array of pointers. 

Let us say you want to store 10 different strings of different lengths and then print the longest string. Instead of using 2 dimensional array of characters - which makes each string of same length and hence wastes space, you can use an array of character pointers.

char *str[10];

Please remember that you should allocate memory for each element of array str.

 for(i=0;i<10;i++)  
 {  
    char temp[30]; int l;  
    fgets(temp,29,stdin);  
    l = strlen(temp);  
    str[i]= malloc(l);  
    strcpy(str[i],temp);  
 }  

You see this array of character pointers used in command line arguments like this

int main(int argc,char *argv[])

When you run  a program, argc will be the number of command line arguments and argv will be an array of character pointers which contain the command line arguments. 

e.g.

./a.out one two three

In this case, argc will be 4 and argv will be an array with ./a.out as argv[0], one as argv[1] and so on. 


You will find many such topics, quizzes and complete programs in my app C interview questions. 
    

Comments

Popular Posts