Lesson 12.1. Introduction to structures in C

Arrays allow to group a set of variables, but only of the same type. In a similar way, a structure allows to define a set of variables, but of different types.

Syntax

Let's suppose that we want to memorize for a student

We then create the following structure:

struct student
{
  char firstname[50];
  char lastname[50];
  float gpa;
};

firstname, lastname and gpa are called the fields of the student structure.

The student structure is actually a new type that we can use in the same way as char, int, double ... It would be more coherent to use the following alternative syntax:

typedef struct
{
  char firstname[50];
  char lastname[50];
  float gpa;
} student;

With this syntax, and in particular the use of the keyword typedef, we understand better that we are creating a new type and not a new variable. In the rest of this course, we will prefer the first syntax which is mostly used.

Declaration of structures

When you have created a new structure, you can declare variables which will be of the type of the structure (here student):

struct student postGraduate;

postGraduate is a variable of the type of the student structure, which will have 3 fields: firstname, name and average.

Field access

Access to the fields of a structure is done, in reading and writing, by adding the name of the field separated by a dot :

// Assign 12.5 to the GPA field
postGraduate.gpa = 12.5;

// Display the last name of the student
printf ("Family name: %s", postGraduate.lastname);

Example

The following example creates a new structure and asks the user to enter some fields before displaying them:

// Declare the student structure
struct student
{
  char firstname[50];
  char lastname[50];
  float gpa;
};

int main(void) {
  // Declare a new student element
  struct student postGraduate;

  // Ask first and last name
  printf("Last name: ");
  scanf ("%s", postGraduate.lastname);
  printf("First name: ");
  scanf ("%s", postGraduate.firstname);

  // Display the name of the student
  printf ("Hello %s %s.\n", postGraduate.firstname, postGraduate.lastname);

  return 0;
}
Last name: Tucky
First name: Ken
Hello Ken Tucky.

Exercises

Exercise 1

Create a date structure that will contain the following fields:

Declare a structure of type date and assign to each field the date of birth of Linus Torvalds (founder of the Linux kernel) before displaying it:

Linus T. was born on 12/28/1969.

Exercise 2

Create a structure named point with two fields:

Create a point array that contains the coordinates of the points of a unit circle with a step of 20 degrees. Recall that the coordinates of the unit circle can be calculated by the following formulas:

$$ \begin{bmatrix} x \\ y \end{bmatrix} = \begin{bmatrix} cos(\alpha) \\ sin(\alpha) \end{bmatrix} $$

Display the points according to this example:

P(0)    = (  1.00 ;  0.00 )
P(1)    = (  0.94 ;  0.34 )
P(2)    = (  0.77 ;  0.64 )
P(3)    = (  0.50 ;  0.87 )
P(4)    = (  0.17 ;  0.98 )
P(5)    = ( -0.17 ;  0.98 )
P(6)    = ( -0.50 ;  0.87 )
P(7)    = ( -0.77 ;  0.64 )
P(8)    = ( -0.94 ;  0.34 )
P(9)    = ( -1.00 ;  0.00 )
P(10)   = ( -0.94 ; -0.34 )
P(11)   = ( -0.77 ; -0.64 )
P(12)   = ( -0.50 ; -0.87 )
P(13)   = ( -0.17 ; -0.98 )
P(14)   = (  0.17 ; -0.98 )
P(15)   = (  0.50 ; -0.87 )
P(16)   = (  0.77 ; -0.64 )
P(17)   = (  0.94 ; -0.34 )
P(18)   = (  1.00 ; -0.00 )

Quiz

In C, what is the purpose of a structure?

Check Bravo! A structure groups variables that can be of different types. Try again...

In C, what do we call the elements composing a structure?

Check Bravo! Each element of a structure is called a field. Try again...

How do you access the fields of a structure?

Check Bravo! The last answer is the syntax for a structure pointer which we will see later. Try again...

Which syntaxes declare a variable of type article?

struct article {
  char name[50];
  float price[50];
  int quantity;
};
Check Bravo! You must specify the struct keyword followed by the name of the structure. Try again...

What does the following code display?

struct point {
  double x; double y;
};
int main(void) {
  struct point P = {12.3, 24.5};
  printf ("%.2lf %.2lf", P.y, P.x);
}
Check Bravo! It is possible to initialize a structure at the declaration in the order of the fields. Try again...

See also


Last update : 11/30/2022