DEV Community

mikkel250
mikkel250

Posted on

2 1

Structures and arrays 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 structure type:

// declares an array of ten items of structure type date
struct date myDates[10];
Enter fullscreen mode Exit fullscreen mode

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

myDates[1].month = 7;
myDates[1].day = 9;
myDates[1].year = 1979;
Enter fullscreen mode Exit fullscreen mode

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:

//assigns the first 3 dates in an array of 6
struct date myDates[6] =
{
  {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};
Enter fullscreen mode Exit fullscreen mode
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, though slightly less readable, would be to use the following notation
struct month aMonth = {31, {'J', 'a', 'n'}};
Enter fullscreen mode Exit fullscreen mode

ACI image

ACI.dev: The Only MCP Server Your AI Agents Need

ACI.dev’s open-source tool-use platform and Unified MCP Server turns 600+ functions into two simple MCP tools on one server—search and execute. Comes with multi-tenant auth and natural-language permission scopes. 100% open-source under Apache 2.0.

Star our GitHub!

Top comments (0)

Tiger Data image

🐯 🚀 Timescale is now TigerData: Building the Modern PostgreSQL for the Analytical and Agentic Era

We’ve quietly evolved from a time-series database into the modern PostgreSQL for today’s and tomorrow’s computing, built for performance, scale, and the agentic future.

So we’re changing our name: from Timescale to TigerData. Not to change who we are, but to reflect who we’ve become. TigerData is bold, fast, and built to power the next era of software.

Read more

👋 Kindness is contagious

Delve into this thought-provoking piece, celebrated by the DEV Community. Coders from every walk are invited to share their insights and strengthen our collective intelligence.

A heartfelt “thank you” can transform someone’s day—leave yours in the comments!

On DEV, knowledge sharing paves our journey and forges strong connections. Found this helpful? A simple thanks to the author means so much.

Get Started