it:informatica:linguaggio_c:struct-typedef_1
Return to Home page
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/it/informatica/linguaggio_c/struct-typedef_1?do=diff&rev2%5B0%5D=1551188125&rev2%5B1%5D=1606429135&difftype=sidebyside
Differences
This shows you the differences between two versions of the page.
— | it:informatica:linguaggio_c:struct-typedef_1 [2024/04/08 22:35] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Struct e Typedef (Esempio 1) ====== | ||
+ | **Concetti: | ||
+ | Costrutti '' | ||
+ | **Testo: | ||
+ | Realizzare un programma che: | ||
+ | * Definisca un nuovo tipo '' | ||
+ | * La struttura dovrà contenere: | ||
+ | * Un campo '' | ||
+ | * Un campo '' | ||
+ | * Un campo '' | ||
+ | * Un vettore di '' | ||
+ | * Il programma, dopo aver dichiarato un vettore di tipo '' | ||
+ | * Dovrà popolare tutti i campi relativi allo studente con indice '' | ||
+ | * Dovrà infine stampare tutti i campi relativi a tale studente | ||
+ | |||
+ | **Soluzione: | ||
+ | <file C struct_typedef_1.c> | ||
+ | /* | ||
+ | Esempio illustrativo del costrutti struct e typedef. | ||
+ | | ||
+ | Il programma dovra': | ||
+ | - Definire un nuovo tipo stud_t basato su una struttura per la memorizzazione dei dati relativi ad uno studente. | ||
+ | - La struttura dovra' contenere: | ||
+ | - Un campo matricola per memorizzare la matricola dello studente | ||
+ | - Un campo nome per la memorizzazione del nome dello studente (lungo al massimo 20 caratteri) | ||
+ | - Un campo media per la memorizzazione della media dei voti dello studente | ||
+ | - Un vettore di voti per la memorizzazione dei voti relativi agli esami sostenuti dallo studente | ||
+ | - Il programma, dopo aver dichiarato un vettore di stud_t atto a memorizzare i dati di N_STUD studenti | ||
+ | - Dovra' popolare tutti i campi relativi allo studente con indice 0 del vettore di tipo stud_t | ||
+ | - Dovra' infine stampare tutti i campi relativi a tale studente | ||
+ | */ | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | #define DIM_NOME 20 | ||
+ | #define N_VOTI 3 | ||
+ | #define N_STUD 10 | ||
+ | |||
+ | /* Definizione del nuovo tipo stud_t */ | ||
+ | typedef struct { | ||
+ | int matricola; | ||
+ | char nome[DIM_NOME+1]; | ||
+ | float media; | ||
+ | int voti[N_VOTI]; | ||
+ | } stud_t; | ||
+ | |||
+ | |||
+ | int main(){ | ||
+ | /* Dichiarazione del vettore di stud_t */ | ||
+ | stud_t studente[N_STUD]; | ||
+ | int i; | ||
+ | | ||
+ | /* Riempimento dei campi della struttura riferiti allo studente con indice 0 */ | ||
+ | strcpy(studente[0].nome, | ||
+ | studente[0].matricola = 13123; | ||
+ | studente[0].media = 23.0; | ||
+ | |||
+ | studente[0].voti[0] = 18; | ||
+ | studente[0].voti[1] = 27; | ||
+ | studente[0].voti[2] = 24; | ||
+ | |||
+ | /* Stampa dei valori precedentemente inseriti */ | ||
+ | printf(" | ||
+ | for(i=0; i< | ||
+ | printf(" | ||
+ | printf(" | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ |
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/it/informatica/linguaggio_c/struct-typedef_1?do=diff&rev2%5B0%5D=1551188125&rev2%5B1%5D=1606429135&difftype=sidebyside
/web/htdocs/www.skenz.it/home/data/pages/it/informatica/linguaggio_c/struct-typedef_1.txt · Last modified: 2024/04/08 22:35 by 127.0.0.1