Home > Software engineering >  Pointer to a pointer C
Pointer to a pointer C

Time:11-21

{   
    char *x = "abc";
    int * y  = &x;
    printf("%s", *(*y));

    return 0;
}

That does not work and I have a hard time figuring out why

Generally, I am just trying to access the string through a pointer to a pointer, but I don't know how

Thank you!

CodePudding user response:

Why it does not work?

int *y is a pointer to int (not pointer to pointer), *y is int. Integer value is not a pointer so it cannot be dereferenced - thus compiler error. I am abstracting from the string aliasing rules. You need to remember to do not dereference pointers using other pointer types. It is UB in C.

int main(void){   
    char *x = "abc";
    char **y  = &x;   // pointer to pointer to char
    printf("`%s`\n", *y);  // single derefence gives pointer to char
    printf("'%c'\n", **y); // double dereference gives char

    return 0;
}

https://www.godbolt.org/z/aoEz57o5d

CodePudding user response:

A pointer stores a memory address. If you have declared a variable and a pointer to that variable, the pointer will store the address of the variable pointed by it. Below, an example with comments.

int a = 1;      // allocated at 0x100, content is 1.
int* ptrA = &a; // allocated at 0x200, content is 0x100 (address of 'a')

In order to access the content of 'a' through the pointer, you have to dereference it, which is done with the dereference operator (*).

printf("'a' content is %d", *ptrA);

A pointer to a pointer also stores a memory address, but in this case, the memory address of another pointer. If you have declared a variable, a pointer to that variable and a pointer to the pointer of that variable, the first pointer still stores the address of the variable as in the first example, and the last pointer stores the address of the first pointer. Below, an example with comments.

int a = 1;               // allocated at 0x100, content is 1.
int* ptrA = &a;          // allocated at 0x200, content is 0x100 (address of 'a')
int** ptrToPtrA = &ptrA; // allocated at 0x300, content is 0x200 (address of 'ptrA')

In order to access the content of 'a' through the pointer to the pointer, you have to dereference it, once for retrieving the content of the pointer to pointer 'ptrToPtrA' (the pointer 'ptrA') and from here, once for retrieving the content of the pointer 'ptrA' (the variable content '1').

printf("'a' content is %d", **ptrToPtrA);

Below, a last example with comments, closer to your original question.

#include <stdio.h>                                                                                                      
                                                                                                                        
int main(void)                                                                                                          
{                                                                                                                       
    // pointer to char (stores the address of a char)                                                                   
    char* x = "abc";                                                                                                    
    printf("'x' address is #%p, content is %c\n", x, *x);                                                               
                                                                                                                        
     // pointer to pointer to char (stores the addres of a pointer to char)                                             
    char** xPtr = &x;                                                                                                                          
    printf("'xPtr' address is #%p, content is %p\n", xPtr, *xPtr);                                                      
    printf("'x' content through 'xPtr' is %c\n", **xPtr);                                                               
                                                                                                                        
    return 0;                                                                                                           
}     
  • Related