User Tools

Site Tools


cs:c_language:struct_and_typedef_1
Return to Home page

Struct and Typedef (Example 1)

Concepts:
Constructs struct and typedef (illustrative example of use)

Text:
Implement a C program that:

  • Defines a new data type with name stud_t based on a structure useful to store information about a student.
  • The structure (identified in a C program with the keyword struct) must contain:
    • a field number to store the identification number of the student
    • a field name to store the name of the student (the student name has a maximum length of 20 characters)
    • a field average to store the average value of the scores of a student
    • a vector of scores to store the scores of the exams attended by the student
  • The program, after declaring a vector (whose elements are of type stud_t) useful to store the data related to N_STUD students.
  • It must fill all the fields related to the student with index 0 in the vector of type stud_t.
  • And finally, it must print all the fields related to the student with index 0 previously inserted.

Solution:

struct_and_typedef_1.c
/*
  Illustrative example of the constructs struct and typedef. 
 
  The program must:
   - Defines a new data type with name ''stud_t'' based on
     a structure useful to store information about a student.
   - The structure (identified in a C program with the keyword ''struct'')
     must contain:
     - a field ''number'' to store the //identification number// of the student
     - a field ''name'' to store the //name// of the student 
       (the student name has a maximum length of 20 characters)
     - a field ''average'' to store the average value of the scores of a student
     - a vector of ''scores'' to store the scores of the exams attended by the student
   - The program, after declaring a vector (whose elements are of type ''stud_t'')
     useful to store the data related to ''N_STUD'' students.
   - It must fill all the fields related to the student with index ''0''
     in the vector of type ''stud_t''.
   - And finally, it must print all the fields related to the student
     with index ''0'' previously inserted.
*/
 
#include <stdio.h>
#include <string.h>
 
#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, "Jules");
  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("%d %s %f\n", student[0].number, student[0].name, student[0].average);
  for(i=0; i<N_SCORES-1; i++)
    printf("%d ", student[0].scores[i]);
  printf("%d\n", student[0].scores[N_SCORES-1]);
 
  return 0;
}

If you found any error, or if you want to partecipate to the editing of this wiki, please contact: wiki [at] altervista.org

You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: http://wiki.altervista.org/cs/c_language/struct_and_typedef_1
/membri/wiki/data/pages/cs/c_language/struct_and_typedef_1.txt · Last modified: 2015/10/28 22:07 by Z


Policy sui Cookie