What is pointer to pointer?

Asked

Viewed 6,497 times

15

How to use it pointer to pointer? I find it hard to tell what it is memory address and memory value when I try to use pointer to pointer, it seems mysterious to me. Someone could give me an explanation and an example of the use of pointer to pointer?

My attempt to implement pointer to pointer:

#include <stdio.h>

int main(void)
{
    //Este código gera um erro. E não exibe o valor da variável "ponteiroDoPonteiro".

    int *ponteiro, **ponteiroDoPonteiro, valor;

    valor = 50;
    ponteiro = &valor;

    *ponteiroDoPonteiro = &ponteiro;

    printf("\n%d", **ponteiroDoPonteiro);

    return 0;
}
  • 6

    In *ponteiroDoPonteiro = &ponteiro; you shouldn’t use the * - So you’re saying that the value of int** (i.e. a int*) must be the address of a int* (i.e. a int**). I can’t give a full answer at the moment, but a reasonable approach is as follows: every time you put a & in front the result type wins a * every time you put a * in front the result type gets a * unless. :)

  • Thanks for the help.

  • 1

    Let’s say every memory cell in the computer corresponds to a house. These houses are all organized in a single street and each house has a number. This house number is the address. Inside each house, there is someone living, and this resident (which is also a number) is the stored value. In the case of a pointer to pointer, we have the number of some house, and when we look inside this house, we find a resident who is a number. The number this resident has represents the number of some other house that has some other resident.

2 answers

11


The error has already been pointed out by the mgibsonbr comment. You cannot store an address (when using the & is saying to pick up the address) as a pointer value. You put the address on the pointer itself (remember that *variavel is saying you want the value pointed by the pointer).

#include <stdio.h>

int main(void) {
    int valor = 50;
    int *ponteiro = &valor;
    int **ponteiroDoPonteiro = &ponteiro; //note que a variável realmente já é um ponteiro
//o que está na posição da memória apontada pela posição da memória de ponteiroDoPonteiro
    printf("\n%d", **ponteiroDoPonteiro);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

The pointer pointer is widely used in vectors where it simulates several dimensions, but the most common use is in the simulation of a vector of strings. If there were the concept of string in the language and the array be treated a little differently, there would be rare cases of use of pointer.

Obviously it can have more levels, but it is more rare to find application for it.

I think the details can be obtained at another answer here on the site referring to another in the OS.

  • Thanks for the explanation, cleared up a little. I had a lot of difficulty understanding this functioning of pointer to pointer.

  • By reference I read about pointer pointer being the same as array array, citing string, even then all well had already thought about it, but had not thought that *****p would be one the same as a dimensional array. Interesting.

10

A pointer is a variable that stores the memory address of another variable, both of the same type.

A pointer to a pointer consists of a variable that stores the address of another, the other in turn stores the address of the variable that has a "common data type". inserir a descrição da imagem aqui

In practice the concept can be seen as follows::

#include <stdio.h>

int main(void)
{
    int *ponteiro, **ponteiroDoPonteiro, valor;
    valor = 50;
    ponteiro = &valor;
    ponteiroDoPonteiro = &ponteiro;
    printf("\nValor de ponteiroDoPonteiro: %p\nEndereço de memoria de ponteiro: %p\nValor de ponteiro: %p\nEndereco de memoria de valor: %p\nValor: %d", ponteiroDoPonteiro, &ponteiro, ponteiro, &valor, valor);
    return 0;
}

The code problem has already been pointed out twice in the comment and in the answer, when trying to do *ponteiroDoPonteiro = &ponteiro; you are trying to put the address of ponteiro in the value of ponteiroDoPonteiro, that does not exist if the same does not point to anything.

Take off the * that you will succeed.

  • Yes I managed to solve. Thanks for the explanation I’m already beginning to understand better the functioning of pointer to pointer.

Browser other questions tagged

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