What is the meaning of double pointer in C? Why do we use pointers in C at all? What is a pointer to a pointer in C? What is pointer to a function?

Basic Concept:-

A pointer is a variable which contains the address in memory of another variable.

#include<stdio.h>

int a = 5;
int *ap = &a;

int main()
{
	printf("Address of a %x \n",ap);
	printf("The value stored at %d is \n",*ap);
	return 0;
}

output:-

Address of a 601040
The value stored at 5 is

  •   ap contains the address of a single variable of integer type.
  •  (*ap) will dereference to the value of the integer.

double pointers in c

double pointers in c

The double pointer in c is denoted by ** asterisk. For example

int **ptr;  in this declaration ptr is a pointer to a pointer.  Let us see an example.

#include<stdio.h>

int id = 503;

int main()
{
	// pointer to integer type
	int *pointer_to_id = &id;

	// declare double pointer
	int **pointer;

	// assign address of pointer_to_id
	pointer = &pointer_to_id;

	// print id
	printf("%d\n",**pointer);

	return 0;
}
  • In the example pointer pointer_to_id stores the address of integer id.
  • The double pointer pointer stores the address of pointer_to_id.
  • **pointer is dereferencing the actual value stored at pointer.
  • The pointer “pointer” can store the address of a pointer variable only.
  • Suppose the variable  id is at location 0x4000.
  • when  we do int *pointer_to_id = &id; it means now pointer_to_id is assigned (0x4000, address of id).
  • As pointer_to_id is also a variable ,a variable must have an address.  let us assume the address of pointer_to_id is 0x6000.
  • so when we do pointer = &pointer_to_id; , then now variable pointer stores the address of pointer_to_id.

Let us discuss more example of double pointer in c

Q.  can you expect the output?

#include<stdio.h>

void assign_value_to_key(char *key)
{
        key = malloc(10);
        strcpy(key,"hello");
}

int main()
{
        char* key = NULL;

        assign_value_to_key(key);
    
        if(key)
        printf("%s\n",key);
        free(key);
        return 0;
}   

Let us compile and see the output:

bosch@bosch-Inspiron-N5050:~$ gcc -g a.c
a.c: In function ‘assign_value_to_key’:
a.c:6:8: warning: incompatible implicit declaration of built-in function ‘malloc’ [enabled by default]
  key = malloc(10);
        ^
a.c:7:2: warning: incompatible implicit declaration of built-in function ‘strcpy’ [enabled by default]
  strcpy(key,"hello");
  ^
bosch@bosch-Inspiron-N5050:~$ ./a.out 
bosch@bosch-Inspiron-N5050:~$

As we may expect the output to be “hello” but why it is not printing ?

Problem:

  • In the function

void assign_value_to_key(char *key)
{
key = malloc(6);
strcpy(key,”hello”);
}

The key is local to function. So  the memory located inside the function will be lost  as soon as the function return to the main(). It also cause a memory leak.

Let us change the code

#include<stdio.h>

void assign_value_to_key(char **key)
{
        *key = malloc(10);
        strcpy(*key,"hello");
}

int main()
{
        char* key = NULL;

        assign_value_to_key(&key);

        if(key)
        printf("%s\n",key);

        free(key);
        return 0;
}

Try  to compile and run,the output should be “hello”

Lesson to learn

  • To modify the value of a variable using pointer in a function, we have to pass address of that variable.
  • So assign_value_to_key(&key); is used to pass address of pointer variable key, and in this case our function void assign_value_to_key(char **key) should use double pointer as a parameter.


Related Contents to follow