Meaning of two asterisks in function call

Asked

Viewed 1,187 times

1

I’m trying to understand a code about simply chained list. The function InserirInicio has as a parameter Nodo **inicio, float dado. I could not understand the use of two asterisks in the parameter in this function in expecifico, would be function requesting a 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);
}
  • 1

    The meaning of ** I understand, my doubt is because it was used in this function in expecifico.

  • It’s a chain list or some other list, right? When you pass the pointer to pointer reference, you will possibly change the position of that pointer being pointed, but keep its contents. Run a paper test simulating the interaction of that code and you’ll find out fast.

2 answers

3


The meaning of ** I understand, my doubt is why it was used in that specific function

The situation here is that the function InserirInicio has to be able to modify its own Nodo that exists in the main, this:

int main()
{
    int i;

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

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

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

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

A solution is instead of passing the pointer, to pass where the pointer is in memory, that is, a pointer to pointer, which is what you see in the code. So you can change the original main making:

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

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

It’s actually not even the only solution. It can also instead of directly changing the main return the new inicio, that would be so:

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, it would return:

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

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

0

I could not understand the use of two asterisks in the parameter in this function in specific, would function requesting a pointer?

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

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

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

Browser other questions tagged

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