Aimed from Aimed C

Posted on

Question :

I have a question about this: **ptr

I can see all other (ptr++, &ptr, *ptr)

I do not know what will be the best way to see how it works **ptr (Pointed from the dotted.)

Thank you to anyone who can clarify this is doubtful.


Answer :

The important part of understanding this is to understand how pointers work.

Let’s imagine that we declare a pointer with the ABC content:
    const char * c=”ABC”;

Then “ABC” is somewhere in memory and the pointer is also in memory. If we want to create a pointer to c, it is also possible with the code:

const char **cp = &c;

In this case the cp pointer is also allocated in memory and is pointing to where pointer c is.

For example, if we look at a piece of memory:

21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30
 A |  B |  C |    | 21 |    | 25 |    |    |

cp está no endereço 27 e aponta para o endereço 25.
c está no endereço 25 e aponta para o endereço 21.
no endereço 21, temos a string ABC.


Imagine you have multiple text pointers

char *lang_pt[] = {"rato", "teclado", "monitor"};
char *lang_en[] = {"mouse", "keyboard", "screen"};

and you want to make a single pointer that points to the language the user chose

char **lang_user;
if (rand() % 2) lang_user = lang_pt;
else            lang_user = lang_en;

printf("2. %sn", lang_user[1]); // teclado???? keyboard???


Quoting Stack Overflow:

  • list of characters (one word): char * word;
  • sentence (a list of words): char ** sentence;
  • monologue (list of phrases): char *** monologue;

And so on …



Leave a Reply

Your email address will not be published. Required fields are marked *