Search code examples
c++pointersdereferencepointer-to-pointer

Not able to understand the notations : * and ** with pointers


I have a problem with the pointers. I know what this does:

*name

I understand that this is a pointer.

I've been searching but I do neither understand what this one does nor I've found helpful information

**name

The context is int **name, not multiplication

Could someone help me?


Solution

  • NOTE: Without the proper context, the usage of *name and **name is ambiguous. it may portrait (a). dereference operator (b) multiplication operator

    Considering you're talking about a scenario like

    • char * name;
    • char **name;

    in the code,

    • *name

    name is a pointer to a char.

    • **name

    name is a pointer, to the pointer to a char.

    Please don't get confused with "double-pointer", which is sometimes used to denote pointer to a pointer but actually supposed to mean a pointer to a double data type variable.

    A visual below

    enter image description here

    As above, we can say

    char value = `c`;
    char *p2 = &value;   // &value is 8000, so p2 == 8000, &p2 == 5000
    char **p1 = &p2;     // &p2 == 5000, p1 == 5000
    

    So, p1 here, is a pointer-to-pointer. Hope this make things clear now.