Pointed from Pointed C

Asked

Viewed 41 times

3

I have a doubt about this : **ptr

I can understand everyone else (ptr++, &ptr, *ptr)

I don’t know the best way to understand how it works **ptr (Pointed.)

Thanks to those who can clarify me is doubt .

3 answers

4


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

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

So, "ABC" is somewhere in the memory and the pointer is also in the memory. If we want to create a pointer to c, it’s 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 the c pointer is.

For example, if we are looking at a chunk of memory:

Endereço
21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30
-----------------------------------------------
 A |  B |  C |    | 21 |    | 25 |    |    |
Conteúdo

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.

3

Imagine you have several 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 has chosen

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

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

2

  • 1

    +1, but lacked a * in his monologue.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.