Sequential Static List

2

Galera look at this code:

lista is struct

lista * crialista()
{
    lista *li;

    li = (lista *) malloc(sizeof(struct lista));

    if(li !=NULL)
    {
        li->qtd = 0 ;
    }
}

Why was lista allocated to li with malloc ? li was declared as lista , this no longer gives access to members of my struct lista ?

    
asked by anonymous 07.07.2015 / 15:44

1 answer

4

When you have declared lista *li you just said that you will have a pointer of type list. It would be the equivalent of saying int *i . This last statement does not say that you have an integer variable, and you can not store any value in i because it is a pointer and not a conventional integer. The same goes for type lista . The *li pointer can not have common values, if not addresses, stored in it. So you need to allocate a space in memory, to store the information in your struct and then point to it with your li pointer.

    
07.07.2015 / 16:04