2014-11-04 8 views
5

All'inizio ho tentato di inizializzare una struttura come questa:Perché ho bisogno di più parentesi graffe durante l'inizializzazione di questa struttura?

struct { 
    char age[2];  // Hold two 1-Byte ages 
} studage[] = { 
    {23, 56}, 
    {44, 26} 
}; 

Ma che mi dà un compilatore avvertimento circa parentesi mancante, quindi ho usato più le parentesi graffe come suggerito dal compilatore e finito con questo:

struct { 
    char age[2];  // Hold two 1-Byte ages 
} studage[] = { 
    {{23, 56}}, 
    {{44, 26}} 
}; 

Nessun avviso. Perché ho bisogno delle parentesi graffe extra?

+5

non è quella esterna per la struttura e quella interna per array di caratteri che è al suo interno? – Wookie88

+0

È un avvertimento, non un errore. Il compilatore non sta "richiedendo" nulla. –

+2

duplicato di [Qual è il significato delle doppie parentesi graffe che inizializzano una C-struct?] (Http://stackoverflow.com/questions/6251160/questo-è-il-misura-del-doppio-curro-braccio-inizializzazione- ac-struct)? – nicael

risposta

10

Si dispone di una matrice di strutture, la struttura ha un membro che è una matrice.

struct { 
    char age[2];  // Hold two 1-Byte ages 
} studage[] = { 
      ^
     This is for the studage array 
    { { 23, 56}}, 
    ^^ 
    | this is for the age array 
    this is for the anonymous struct 

    {{44, 26}} 
}; 

Forse è più facile vedere se il vostro struct aveva un altro membro:

struct { 
     int id; 
     char age[2]; 
    } studage[] = { 
     {1, {23, 56}}, 
     ^^^
     id | | 
      age[0] | 
       age[1] 
    }; 
Problemi correlati