Sequential Static List

Asked

Viewed 610 times

2

Guys look at this following code :

lista is a struct

lista * crialista()
{
    lista *li;

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

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

because it was allocated lista in li with malloc ? whereas li was declared as lista , that no longer gives access to members of my struct lista?

  • 1

    lista *li defines a pointer. But that pointer does not point anywhere defined! If you prefer a real object, you can do lista li which is a real object with uncirculated values; with a pointer you have to point it to a specific place (with malloc()) and then assign values to the elements.

1 answer

4


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

Browser other questions tagged

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