loading...

Structures in C

mikkel250 profile image mikkel250 ・2 min read

Basics of the C programming language (20 Part Series)

1) Functions in C 2) Arrays in C 3 ... 18 3) Strings in C 4) String functions in C 5) Setting up VSCode for C in linux 6) Pointer basics in C 7) Advanced pointer operations in C 8) Pointers: Const, Void, and Arrays 9) Pointers arithmetic 10) Pointers and strings 11) Passing by value, passing by reference in C 12) Dynamic memory allocation in C 13) Structures in C 14) Structures and arrays in C 15) Nested structures in C 16) Structures and pointers in C 17) Structures and functions in C 18) File input and output in C 19) Reading file contents in C 20) Writing to files in C

Overview

As covered in the previous post, structures are very useful for logically grouping related elements together. This can be further extended by combining them with arrays. Since structures are a type, it is perfectly valid in C to create an array of structures. So, if you had to keep track of many dates (to continue with the previous examples), an array could be created to hold all of them in one place.

Declaring an array of structures

To declare an array of structures, the syntax is similar to declaring as any other array, but preceded by the struct keyword and struct type:

// declares an array of ten items of structure type date
struct date myDates[10]

To access or assign elements inside the array, use the index and the dot notation:

myDates[1].month = 7;
myDates[1].day = 9;
myDates.year = 1979;

Initializing of arrays containing structures is similar to initializing multidimensional arrays. The internal curly brackets below are optional, but they make the code much more readable, so are recommended:

struct date myDates[3] =
{
  {7, 9, 1979},
  {11, 26, 2019},
  {12, 26, 2019},
};

  // to initialize values besides the one declared in the initial initialization, the curly brackets can be used inside the declaration
struct date myDates[3] =
{
  [4] = {1, 1, 2020}
};

// ...and dot notation can be used to assign specific elements as well
struct date myDates[3] = {[1].month = 12, [1].day =30};
Structures that contain arrays

It is also possible to define structures that contain arrays as members. The most common use case of this is to have strings inside a structure, e.g. creating a structure named 'month' that contains both the number of days and the abbreviation for the name of the month:

struct month
{
  int numberOfDays;
  char name[3];
}

struct month aMonth;
aMonth.numberOfDays = 31;
aMonth.name[0] = 'J';
aMonth.name[1] = 'a';
aMonth.name[2] = 'n';

// a quicker way to do the above for the name would be to use a shortcut
struct month aMonth = {31, {'J', 'a', 'n'}};

Basics of the C programming language (20 Part Series)

1) Functions in C 2) Arrays in C 3 ... 18 3) Strings in C 4) String functions in C 5) Setting up VSCode for C in linux 6) Pointer basics in C 7) Advanced pointer operations in C 8) Pointers: Const, Void, and Arrays 9) Pointers arithmetic 10) Pointers and strings 11) Passing by value, passing by reference in C 12) Dynamic memory allocation in C 13) Structures in C 14) Structures and arrays in C 15) Nested structures in C 16) Structures and pointers in C 17) Structures and functions in C 18) File input and output in C 19) Reading file contents in C 20) Writing to files in C

Posted on Dec 24 '19 by:

mikkel250 profile

mikkel250

@mikkel250

I've been tinkering with computers since I was a teen. I'm currently pivoting from my current role as Tech Support manager to Full Stack Web Developer. I'm actively seeking employment in the field.

Discussion

markdown guide