Struct con Array e Altre Struct
In alcune strutture, diventa necessario utilizzare un array di strutture annidate quando un oggetto contiene più sotto-oggetti correlati. Ad esempio, se uno studente può frequentare diversi corsi, ha senso rappresentare i corsi come un array di strutture all'interno della struttura Student.
Sintassi di esempio:
OuterStruct.NestedStruct[1].NestedField;
Per accedere a un elemento dell'array di strutture annidate, si fa prima riferimento alla struttura esterna, poi si utilizza un indice per selezionare l'elemento desiderato dell'array e infine si accede a un campo specifico di quell'elemento.
Esempio pratico
Si immagini che ogni studente abbia un nome, un'età e un insieme di corsi. Ogni corso contiene il suo titolo, il docente e il numero di crediti.
main.c
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152#include <stdio.h> #include <string.h> struct Course { char name[50]; char instructor[50]; int credits; }; struct Student { char name[50]; int age; struct Course courses[3]; // array of nested structures }; int main() { struct Student student; // Initialize student strcpy(student.name, "Alice Johnson"); student.age = 20; // Initialize courses strcpy(student.courses[0].name, "Mathematics"); strcpy(student.courses[0].instructor, "Dr. Smith"); student.courses[0].credits = 4; strcpy(student.courses[1].name, "Computer Science"); strcpy(student.courses[1].instructor, "Prof. Brown"); student.courses[1].credits = 3; strcpy(student.courses[2].name, "History"); strcpy(student.courses[2].instructor, "Dr. Green"); student.courses[2].credits = 2; // Display information printf("Student: %s\n", student.name); printf("Age: %d\n", student.age); printf("Courses:\n"); for (int i = 0; i < 3; i++) { printf(" Course %d: %s, Instructor: %s, Credits: %d\n", i + 1, student.courses[i].name, student.courses[i].instructor, student.courses[i].credits); } // Example of accessing a specific nested field printf("\nInstructor of the second course: %s\n", student.courses[1].instructor); return 0; }
In questo esempio, la struttura Student contiene un array courses con tre elementi di tipo Course. Ogni corso ha il proprio titolo, docente e numero di crediti. Per accedere a un corso specifico, utilizzare l'indice dell'array e l'operatore punto: student.courses[1].instructor.
L'utilizzo di array di strutture annidate è utile per rappresentare oggetti con sotto-oggetti ripetuti, come studenti con corsi, negozi con prodotti o libri con autori.
Swipe to start coding
Lo studente è rappresentato da una struttura Student che contiene un array a dimensione fissa di strutture annidate Course. Ogni corso ha un nome, un docente e un numero di crediti. La funzione deve iterare su tutti i corsi e sommare i crediti.
Implementare una funzione calculateTotalCredits con tipo di ritorno int.
- All'interno della funzione, creare una variabile
intchiamatatotalinizializzata a0. - Utilizzare un ciclo
forper iterare da0al numero di corsi (3). - Per ogni corso, accedere a
creditse aggiungerlo atotal. - Restituire il valore finale di
total.
Soluzione
Grazie per i tuoi commenti!
single
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Can you show me a code example of how to define these nested structures?
How do I access or modify a specific course for a student?
What are some other real-world scenarios where nested structure arrays are useful?
Fantastico!
Completion tasso migliorato a 4.35
Struct con Array e Altre Struct
Scorri per mostrare il menu
In alcune strutture, diventa necessario utilizzare un array di strutture annidate quando un oggetto contiene più sotto-oggetti correlati. Ad esempio, se uno studente può frequentare diversi corsi, ha senso rappresentare i corsi come un array di strutture all'interno della struttura Student.
Sintassi di esempio:
OuterStruct.NestedStruct[1].NestedField;
Per accedere a un elemento dell'array di strutture annidate, si fa prima riferimento alla struttura esterna, poi si utilizza un indice per selezionare l'elemento desiderato dell'array e infine si accede a un campo specifico di quell'elemento.
Esempio pratico
Si immagini che ogni studente abbia un nome, un'età e un insieme di corsi. Ogni corso contiene il suo titolo, il docente e il numero di crediti.
main.c
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152#include <stdio.h> #include <string.h> struct Course { char name[50]; char instructor[50]; int credits; }; struct Student { char name[50]; int age; struct Course courses[3]; // array of nested structures }; int main() { struct Student student; // Initialize student strcpy(student.name, "Alice Johnson"); student.age = 20; // Initialize courses strcpy(student.courses[0].name, "Mathematics"); strcpy(student.courses[0].instructor, "Dr. Smith"); student.courses[0].credits = 4; strcpy(student.courses[1].name, "Computer Science"); strcpy(student.courses[1].instructor, "Prof. Brown"); student.courses[1].credits = 3; strcpy(student.courses[2].name, "History"); strcpy(student.courses[2].instructor, "Dr. Green"); student.courses[2].credits = 2; // Display information printf("Student: %s\n", student.name); printf("Age: %d\n", student.age); printf("Courses:\n"); for (int i = 0; i < 3; i++) { printf(" Course %d: %s, Instructor: %s, Credits: %d\n", i + 1, student.courses[i].name, student.courses[i].instructor, student.courses[i].credits); } // Example of accessing a specific nested field printf("\nInstructor of the second course: %s\n", student.courses[1].instructor); return 0; }
In questo esempio, la struttura Student contiene un array courses con tre elementi di tipo Course. Ogni corso ha il proprio titolo, docente e numero di crediti. Per accedere a un corso specifico, utilizzare l'indice dell'array e l'operatore punto: student.courses[1].instructor.
L'utilizzo di array di strutture annidate è utile per rappresentare oggetti con sotto-oggetti ripetuti, come studenti con corsi, negozi con prodotti o libri con autori.
Swipe to start coding
Lo studente è rappresentato da una struttura Student che contiene un array a dimensione fissa di strutture annidate Course. Ogni corso ha un nome, un docente e un numero di crediti. La funzione deve iterare su tutti i corsi e sommare i crediti.
Implementare una funzione calculateTotalCredits con tipo di ritorno int.
- All'interno della funzione, creare una variabile
intchiamatatotalinizializzata a0. - Utilizzare un ciclo
forper iterare da0al numero di corsi (3). - Per ogni corso, accedere a
creditse aggiungerlo atotal. - Restituire il valore finale di
total.
Soluzione
Grazie per i tuoi commenti!
single