Create Threaded Dynamic List

1

I'm learning how to work with linked lists. In one of the video lessons 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;

So I understand li is a pointer to "list head". The part that is breaking my head is in the boot function. It creates in memory a space of a pointer to list and allocates in aux , however how can *aux=NULL if aux still not point anywhere ?? I do not know if you could understand my doubt, if anyone wants to watch the video I mentioned above, follow the link: Video Thread Chained List

    
asked by anonymous 16.04.2018 / 05:53

1 answer

2

I start by saying that I'm not particularly fond of doubles typedefs for the same type being one pointer and another 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 typedef . This may mislead me to use the pointer directly without allocating memory.

That said, let's move on to the question:

  

How can you *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 statement you are saving the memory location returned by malloc , so you can say that aux already points to a memory location. It would be the same if it 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 the 2% with% mentioned above, because you actually have a double hand pointer, a typedefs which is a Lista* . So for the example I gave up 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. Elem** is a pointer that points to a pointer (double pointer). It is in this case to say that the base pointer does not point at all:

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

Explaining in Portuguese. x is a double integer pointer, so it points to a x integer pointer. But at this point you are pointing to int* which means nothing.

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

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

It would look like this:

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

Now, using only the first NULL could have written the statement this way:

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

That would be closer to the last example I gave.

As a last resort, I kept casts to make the example look like it had, but they are unnecessary. Soon the original instruction could be written like this:

aux = malloc(sizeof(Lista));

Getting simpler.

    
16.04.2018 / 11:52