Wednesday, July 4, 2018

C Pointer to Pointer

In C pointer to pointer concept, a pointer refers to the address of another pointer.

In c language, a pointer can point to the address of another pointer which points to the address of a value. Let's understand it by the diagram given below:

pointer to pointer in c

Let's see the syntax of pointer to pointer.





int **p2;



C pointer to pointer

Till now we have used or learned pointer to a data type like character, integer etc. But in this section we will learn about pointers pointing to pointers.

As the definition of pointer says that its a special variable that can store the address of an other variable. Then the other variable can very well be a pointer. This means that its perfectly legal for a pointer to be pointing to another pointer.

Lets suppose we have a pointer ‘p1’ that points to yet another pointer ‘p2’ that points to a character ‘ch’. In memory, the three variables can be visualized as :

C pointer to pointer example

As you can see in the above figure, p2 contains the address of p (fff2) and p contains the address of number variable (fff4).





#include<stdio.h>
int main(){
int number=50;
int *p;//pointer to int
int **p2;//pointer to pointer
p=&number;//stores the address of number variable
p2=&p;
printf("Address of number variable is %x \n",&number);
printf("Address of p variable is %x \n",p);
printf("Value of *p variable is %d \n",*p);
printf("Address of p2 variable is %x \n",p2);
printf("Value of **p2 variable is %d \n",*p);
return 0;
}



Output

Address of number variable is fff4

Address of p variable is fff4

Value of *p variable is 50

Address of p2 variable is fff2

Value of **p variable is 50

C Pointer to Constant

This concept is easy to understand as the name simplifies the concept. Yes, as the name itself suggests, this type of pointer cannot change the value at the address pointed by it.

Lets understand this through an example :





char ch = 'c';
char *ptr = &ch
*ptr = 'a';



In the above example, we used a character pointer ‘ptr’ that points to character ‘ch’. In the last line, we change the value at address pointer by ‘ptr’. But if this would have been a pointer to a constant, then the last line would have been invalid because a pointer to a constant cannot change the value at the address its pointing to.

A pointer to a constant is declared as :





const *;








#include<stdio.h>

int main(void)
{
char ch = 'c';
const char *ptr = &ch; // A constant pointer 'ptr' pointing to 'ch'
*ptr = 'a';// WRONG!!! Cannot change the value at address pointed by 'ptr'.

return 0;
}



When the above code was compiled, compiler gave the following error :





$ gcc -Wall ptr2const.c -o ptr2const
ptr2const.c: In function ‘main’:
ptr2const.c:7: error: assignment of read-only location ‘*ptr’



So now we know the reason behind the error above ie we cannot change the value pointed to by a constant pointer.

C Function Pointers

Just like pointer to characters, integers etc, we can have pointers to functions.

A function pointer can be declared as :

(*) (type of function arguments)

For example :






int (*fptr)(int, int)



The above line declares a function pointer ‘fptr’ that can point to a function whose return type is ‘int’ and takes two integers as arguments.

Lets take a working example :





#include<stdio.h>

int func (int a, int b)
{
printf("\n a = %d\n",a);
printf("\n b = %d\n",b);

return 0;
}

int main(void)
{
int(*fptr)(int,int); // Function pointer

fptr = func; // Assign address to function pointer

func(2,3);
fptr(2,3);

return 0;
}



In the above example, we defined a function ‘func’ that takes two integers as inputs and returns an integer. In the main() function, we declare a function pointer ‘fptr’ and then assign value to it. Note that, name of the function can be treated as starting address of the function so we can assign the address of function to function pointer using function’s name. Lets see the output :


$ ./fptr

a = 2

b = 3

a = 2

b = 3

So from the output we see that calling the function through function pointer produces the same output as calling the function from its name.

To conclude, in this article we touched some of the advanced concepts related to pointers. There can be some interesting problems related to pointers, which we might cover in some future article.




Instagram





No comments:

Post a Comment