cs:c_language:struct_and_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/cs/c_language/struct_and_typedef_1?rev=1551188114&do=diff
Differences
This shows you the differences between two versions of the page.
— | cs:c_language:struct_and_typedef_1 [2024/04/08 22:35] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Struct and Typedef (Example 1) ====== | ||
+ | **Concepts: | ||
+ | Constructs '' | ||
+ | **Text:**\\ | ||
+ | Implement a C program that: | ||
+ | * Defines a new data type with name '' | ||
+ | * The structure (identified in a C program with the keyword '' | ||
+ | * a field '' | ||
+ | * a field '' | ||
+ | * a field '' | ||
+ | * a vector of '' | ||
+ | * The program, after declaring a vector (whose elements are of type '' | ||
+ | * It must fill all the fields related to the student with index '' | ||
+ | * And finally, it must print all the fields related to the student with index '' | ||
+ | |||
+ | **Solution: | ||
+ | <file C struct_and_typedef_1.c> | ||
+ | /* | ||
+ | Illustrative example of the constructs struct and typedef. | ||
+ | | ||
+ | The program must: | ||
+ | - Defines a new data type with name '' | ||
+ | a structure useful to store information about a student. | ||
+ | - The structure (identified in a C program with the keyword '' | ||
+ | must contain: | ||
+ | - a field '' | ||
+ | - a field '' | ||
+ | (the student name has a maximum length of 20 characters) | ||
+ | - a field '' | ||
+ | - a vector of '' | ||
+ | - The program, after declaring a vector (whose elements are of type '' | ||
+ | | ||
+ | - It must fill all the fields related to the student with index '' | ||
+ | in the vector of type '' | ||
+ | - And finally, it must print all the fields related to the student | ||
+ | with index '' | ||
+ | */ | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | #define DIM_NAME 20 | ||
+ | #define N_SCORES 3 | ||
+ | #define N_STUD 10 | ||
+ | |||
+ | /* Definition of the new type stud_t */ | ||
+ | typedef struct { | ||
+ | int number; | ||
+ | char name[DIM_NAME+1]; | ||
+ | float average; | ||
+ | int scores[N_SCORES]; | ||
+ | } stud_t; | ||
+ | |||
+ | |||
+ | int main(){ | ||
+ | /* Declaration of the vector of stud_t */ | ||
+ | stud_t student[N_STUD]; | ||
+ | int i; | ||
+ | | ||
+ | /* Filling of the fields of the structure referred to the student | ||
+ | index with index 0 of the vector */ | ||
+ | strcpy(student[0].name, | ||
+ | student[0].number = 13123; | ||
+ | student[0].average = 23.0; | ||
+ | |||
+ | student[0].scores[0] = 18; | ||
+ | student[0].scores[1] = 27; | ||
+ | student[0].scores[2] = 24; | ||
+ | |||
+ | /* Printing of the values previously written */ | ||
+ | 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/cs/c_language/struct_and_typedef_1?rev=1551188114&do=diff