Create Chained Dynamic List

Asked

Viewed 677 times

1

I am learning how to work with chained lists. In one of the videos classes I attended, the teacher suggests the following code:

struct celula {
      int info;
      struct celula *prox;
};
typedef celula Elem;
typedef celula *Lista;

Lista* iniciaLista(){
       Lista *aux;
       aux=(Lista*) malloc(sizeof(Lista));
       *aux=NULL;
       return aux;
}

int main(){
    Lista *li;
    li=iniciaLista;

From what I understand li is a pointer to "list head". The part that’s breaking my head is in the boot function. It creates in memory a space from a pointer to list and allocates in aux, but how is it possible to do *aux=NULL if aux Still not pointing anywhere? I do not know if I understand my doubt, in case someone wants to watch the video I mentioned above, follow the link: Video Lesson Chained List

1 answer

2


Let me start by saying that I’m not particularly fond of stuntmen typedefs for the same type being that one is pointer and the other is the normal structure:

typedef celula Elem;
typedef celula *Lista;

This means that Elem and Lista correspond to celula and celula* respectively. However when I do:

Lista li;

It is not particularly clear that li is a pointer unless I go see the typedef. This may mislead me to use the pointer directly without allocating memory.

Having said that, let us move on to the question:

how is it possible to do *aux=NULL if aux still does not point to place none??

But aux points to a place, the place returned by malloc:

aux=(Lista*) malloc(sizeof(Lista));

Note that in this instruction you are saving the memory location returned by malloc, so you can say that aux already points to a memory position. It would be the same if you had a int* for example:

int* x = (int*) malloc(sizeof(int)); //guardar o endereço devolvido por malloc
*x = 10; //no lugar apontado por x coloca o valor 10

The difficulty probably comes from those 2 typedefs that I mentioned above, because in fact you have a double pointer in hand, a Lista* which is a Elem**. So for the example I gave above to better reflect your reality would have to be written like this:

int** x = (int**) malloc(sizeof(int*));
*x = NULL;

Look calmly at this instruction. x is a pointer that points to a pointer (double pointer). You are in this case saying that the base pointer does not point to anything:

 ----      
| x  | --> NULL
 ----      

Explaining in Portuguese. x is a double integer pointer, and so it points to a int* an integer pointer. But right now it is pointing to NULL that means nothing.

In a case that points to something valid, starting for example from this code:

int** x = (int**) malloc(sizeof(int*));
int* y = (int*) malloc(sizeof(int));
*x = y;
*y = 20;

Would look like this:

 ----       ----       ----
| x  | --> | y  | --> | 20 |
 ----       ----       ---- 

By the way, using only the first typedef could have written the instruction this way:

aux = (Elem**) malloc(sizeof(Elem*));

That would be closer to this last example I gave.

As last aside I kept the Casts to make the example look like what they had, but they are unnecessary. Soon could the original instruction be written like this:

aux = malloc(sizeof(Lista));

Getting simpler.

  • Very good, thanks too much!!

Browser other questions tagged

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