Meaning of two asterisks in function call [duplicate]

1

I'm trying to understand a code on list simply chained. The function InserirInicio is set to Nodo **inicio, float dado . I could not understand the use of two asterisks in the parameter in this function in specific, would be function requesting a pointer pointer?

Struct and function:

typedef struct nodo
{
    float dado;
    struct nodo *proximo;
} Nodo;

int InserirInicio(Nodo **inicio, float dado) {
    Nodo *nodo;
    if ((nodo = (Nodo *) malloc(sizeof(Nodo)))  == NULL)
      return 0;

    nodo->dado    = dado;
    nodo->proximo = NULL;

    if (*inicio != NULL)
        nodo->proximo = *inicio;

    *inicio = nodo;
    return 1;

}

Function call:

int main()
{
    int i;

    Nodo *inicio = NULL;


    for(i = 0; i < 8; i++)
    {
        InserirInicio(&inicio, i * 15.0);
    }
return(0);
}
    
asked by anonymous 22.12.2017 / 00:50

2 answers

3
  

The meaning of ** to understand, my doubt is because it was used   in this specific function

The situation here is that the InserirInicio function must be able to modify the Nodo itself that exists in main , this:

int main()
{
    int i;

    Nodo *inicio = NULL; //<--este

What will happen only when it is NULL , but in C all function parameters are copies, so if the InserirInicio function is this way:

int InserirInicio(Nodo *inicio, float dado) {
    ...
    inicio = nodo;

You are actually changing the parameter of the function and not the pointer that exists in main .

One solution is instead of passing the pointer, passing the place where the pointer is in memory, that is, a pointer to pointer, which you see in the code. So you can change the original of main by doing:

int InserirInicio(Nodo **inicio, float dado) {
    ...

    *inicio = nodo; //alterar o inicio que está no main

In fact, it's not even the only solution. You can also instead of directly change the main return the new inicio , which would look like this:

int main() {
    ...

    for(i = 0; i < 8; i++)
    {
        inicio = InserirInicio(inicio, i * 15.0); //sem & e guardando em inicio
    }

And in function instead of affecting, would return:

Node* InserirInicio(Nodo *inicio, float dado) {
    ...
    return nodo;
}

Note that in this case the parameter is only Nodo* , but the return is also of type Nodo*

    
22.12.2017 / 01:33
0
  

I could not understand the use of two asterisks in the parameter in that   function, what would be the function requesting a pointer pointer?

That's exactly it. This InserirInicio function requires in one of its arguments, a value of type pointer pointer Nodo ( Nodo ** ). It requires this type of argument to be able to, for example, be able to change the value of a pointer variable Nodo ( Nodo * ) that was declared out of the InserirInicio function itself. >

In these codes you passed, the inicio variable that was declared in the main function (not to be confused with the inicio variable that is the InserirInicio function parameter) is the variable that has its value read and then changed within the InserirInicio function, although it was not declared in that function. This change in value within InserirInicio occurs exactly at line *inicio = nodo; , and stores a new pointer in the variable inicio that was declared in main .

And that's the kind of thing you get using a pointer pointer.

    
22.12.2017 / 09:24