Domanda

Ho definito questa struttura

typedef  struct UNIAO_NOME
{
     int vert;
     struct un *Pai; 
} un;

ma, poi, quando ci provo

int name (un *conj/*pointer to a subset*/)
{
    un c;/*subset*/ 
    c = *conj;
    while (c.Pai != NULL)
        c = *(c.Pai);
    return c.vert;
}

l'editore dice che

c = *(c.Pai); 

è sbagliato, tuttavia c è di tipo un.

Grazie in anticipo.

È stato utile?

Soluzione

Hai due tipi diversi - all'interno del struct, hai dichiarato un nuovo tipo un .Scrivilo come questo:

typedef struct UNIAO_NOME
{
     int vert;
     struct UNIAO_NOME *Pai;    // refers to this type
} un;
.

Altri suggerimenti

Non si definisce mai un struct un ma solo un identificatore un (senza struct) che si riferisce al tipo struct UNIAO_NOME.

Servendosi struct un* è in realtà una "dichiarazione diretta" implicita di struct un.Per il compilatore questo è sufficiente per dedurre quanto grande sarà un puntatore a una struttura non ancora definita.

Un idioma che può essere utilizzato per tali tipi di mal di testa è quello di typedef tutte le tue strutture prima la loro dichiarazione completa:

typedef struct un un;

avanti dicembre ^^^^^^^^^^^
tipo alias ------------------^^

struct un
{
     int vert;
     un *Pai; 
};
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top