Serie di strutture in C

Quando si ha a che fare con un ampio insieme di dati correlati e di diversi tipi di dati, organizzarli e gestirli in modo efficiente è fondamentale. Nella programmazione C, la combinazione di array e strutture, ovvero array di strutture, fornisce un potente strumento per gestirlo. In questo articolo, discutiamo il concetto di array di strutture in C.

Cos'è l'Array?

L'array è una raccolta omogenea di elementi archiviati nella posizione di memoria continua. La dimensione dell'array è fissa e possiamo accedere in modo casuale agli elementi utilizzando il loro indice.

Dichiarazione di matrice

array_type   array_name  [size]; 

Cos'è la struttura?

La struttura è uno dei tipi di dati definiti dall'utente in C che può contenere elementi di tipi diversi come membri.

Dichiarazione di una struttura in C

  struct   structure_name{    memberType memberName;   ...  ... }; 

Matrice di strutture

Un array i cui elementi sono di tipo struttura è chiamato array di struttura. È generalmente utile quando abbiamo bisogno di più variabili di struttura nel nostro programma.

Necessità di una serie di strutture

Supponiamo di avere 50 dipendenti e di dover archiviare i dati di 50 dipendenti. Quindi, per questo, dobbiamo definire 50 variabili di tipo struct Employee e archiviare i dati al suo interno. Tuttavia, dichiarare e gestire le 50 variabili non è un compito facile. Immaginiamo uno scenario più grande, come 1000 dipendenti.

Quindi, se dichiariamo la variabile in questo modo, non è possibile gestirla.

struct Employee emp1, emp2, emp3, .. . ... . .. ... emp1000; 

Per questo, possiamo definire un array il cui tipo di dati sarà struct Employee, così che sarà facilmente gestibile.

Dichiarazione di matrice di strutture

  struct     structure_name array_name   [number_of_elements]; 

Inizializzazione di array di strutture

Possiamo inizializzare l'array di strutture nei seguenti modi:

  struct     structure_name array_name   [number_of_elements] = {  {element1_value1, element1_value2, ....},  {element2_value1, element2_value2, ....},  ......  ...... }; 

La stessa inizializzazione può essere eseguita anche come:

  struct     structure_name array_name   [number_of_elements] = {  element1_value1, element1_value2 ....,  element2_value1, element2_value2 ..... }; 

I compilatori GNU C supportano l'inizializzazione designata per le strutture, quindi possiamo usarla anche nell'inizializzazione di un array di strutture.

  struct     structure_name array_name   [number_of_elements] = {  {.element3 = value, .element1 = value, ....},  {.element2 = value, .elementN = value, ....},  ......  ...... }; 

Esempio di array di strutture in C

C




// C program to demonstrate the array of structures> #include> > // structure template> struct> Employee {> > char> Name[20];> > int> employeeID;> > int> WeekAttendence[7];> };> > // driver code> int> main()> {> > // defining array of structure of type Employee> > struct> Employee emp[5];> > > // adding data> > for> (> int> i = 0; i <5; i++) {> > emp[i].employeeID = i;> > strcpy> (emp[i].Name,> 'Amit'> );> > int> week;> > for> (week = 0; week <7; week++) {> > int> attendence;> > emp[i].WeekAttendence[week] = week;> > }> > }> > printf> (> ' '> );> > > // printing data> > for> (> int> i = 0; i <5; i++) {> > printf> (> 'Emplyee ID: %d - Employee Name: %s '> ,> > emp[i].employeeID, emp[i].Name);> > printf> (> 'Attendence '> );> > int> week;> > for> (week = 0; week <7; week++) {> > printf> (> '%d '> , emp[i].WeekAttendence[week]);> > }> > printf> (> ' '> );> > }> > > return> 0;> }>

Produzione

Emplyee ID: 0 - Employee Name: Amit Attendence 0 1 2 3 4 5 6  Emplyee ID: 1 - Employee Name: Amit Attendence 0 1 2 3 4 5 6  Emplyee ID: 2 - Employee Name: Amit Attendence 0 1 2 3 4 5 6  Emplyee ID: 3 - Employee Name: Amit Attendence 0 1 2 3 4 5 6  Emplyee ID: 4 - Employee Name: Amit Attendence 0 1 2 3 4 5 6