C Structures: Grouping Related Variables with Structs

Structures, commonly referred to as structs, enable you to group several related variables together in C programming. Each variable within a structure is called a member, and unlike arrays, a structure can accommodate different data types such as int, float, and char. This flexibility makes structs a powerful tool for organizing data in your programs.



C Structures (Structs)

Structures

Structures, also known as structs, allow you to group several related variables together. Each variable within a structure is called a member, and unlike arrays, a structure can contain different data types such as int, float, and char.

Create a Structure

You can create a structure using the struct keyword, followed by the members defined inside curly braces:

Example

struct MyStructure {   // Structure declaration
  int myNum;           // Member (int variable)
  char myLetter;       // Member (char variable)
}; // End the structure with a semicolon

To use the structure, create a variable of that type:

Example

struct myStructure {
  int myNum;
  char myLetter;
};

int main() {
  struct myStructure s1; // Create a struct variable
  return 0;
}

Access Structure Members

You can access members of a structure using the dot syntax (.):

Example

// Create a structure called myStructure
struct myStructure {
  int myNum;
  char myLetter;
};

int main() {
  struct myStructure s1; // Create a struct variable

  // Assign values to members of s1
  s1.myNum = 13;
  s1.myLetter = 'B';

  // Print values
  printf("My number: %d\n", s1.myNum);
  printf("My letter: %c\n", s1.myLetter);

  return 0;
}
Output

My number: 13
My letter: B

You can create multiple structure variables with different values using the same structure:

Example

// Create different struct variables
struct myStructure s1;
struct myStructure s2;

// Assign values to different struct variables
s1.myNum = 13;
s1.myLetter = 'B';

s2.myNum = 20;
s2.myLetter = 'C';

What About Strings in Structures?

In C, strings are essentially arrays of characters. However, you cannot assign a value to a string member directly:

Example

struct myStructure {
  int myNum;
  char myLetter;
  char myString[30];  // String
};

int main() {
  struct myStructure s1;

  // Trying to assign a value to the string
  s1.myString = "Some text"; // This will cause an error

  // Print the value
  printf("My string: %s", s1.myString);

  return 0;
}

This will result in an error:


prog.c:12:15: error: assignment to expression with array type

To assign a value to a string member, use the strcpy() function:

Example

#include <string.h> // Include string.h for strcpy function

struct myStructure {
  int myNum;
  char myLetter;
  char myString[30]; // String
};

int main() {
  struct myStructure s1;

  // Assign a value to the string using strcpy function
  strcpy(s1.myString, "Some text");

  // Print the value
  printf("My string: %s", s1.myString);

  return 0;
}
Output

My string: Some text

Simpler Syntax

You can also assign values to structure members at the time of declaration using a single line. Just insert the values in a comma-separated list inside curly braces:

Example

struct myStructure {
  int myNum;
  char myLetter;
  char myString[30];
};

int main() {
  // Create a structure variable and assign values to it
  struct myStructure s1 = {13, 'B', "Some text"};

  // Print values
  printf("%d %c %s", s1.myNum, s1.myLetter, s1.myString);

  return 0;
}
Output

13 B Some text

Note that the order of the values must match the order of the variable types declared in the structure.

Copy Structures

You can also assign one structure to another:

Example

struct myStructure s1 = {13, 'B', "Some text"};
struct myStructure s2;

// Copy s1 values to s2
s2 = s1;

Modify Values

To modify a value, you can again use the dot syntax (.). To change a string value, use strcpy():

Example

#include <string.h> // Include string.h for strcpy function

struct myStructure {
  int myNum;
  char myLetter;
  char myString[30];
};

int main() {
  struct myStructure s1 = {13, 'B', "Some text"};

  // Modify values
  s1.myNum = 30;
  s1.myLetter = 'C';
  strcpy(s1.myString, "Something else");

  // Print values
  printf("%d %c %s", s1.myNum, s1.myLetter, s1.myString);

  return 0;
}
Output

30 C Something else

Modifying values can be especially useful when you copy structure values:

Example

// Create a structure variable and assign values to it
struct myStructure s1 = {13, 'B', "Some text"};

// Create another structure variable
struct myStructure s2;

// Copy s1 values to s2
s2 = s1;

// Change s2 values
s2.myNum = 30;
s2.myLetter = 'C';
strcpy(s2.myString, "Something else");

// Print values
printf("%d %c %s\n", s1.myNum, s1.myLetter, s1.myString);
printf("%d %c %s\n", s2.myNum, s2.myLetter, s2.myString);

How Are Structures Useful?

Structures are especially useful when you need to store related information about an entity. For example, if you want to store details about cars like brand, model, and year, you can create a "Car template" structure:

Example

struct Car {
  char brand[50];
  char model[50];
  int year;
};

int main() {
  struct Car car1 = {"BMW", "X5", 1999};
  struct Car car2 = {"Ford", "Mustang", 1969};
  struct Car car3 = {"Toyota", "Corolla", 2011};

  printf("%s %s %d\n", car1.brand, car1.model, car1.year);
  printf("%s %s %d\n", car2.brand, car2.model, car2.year);
  printf("%s %s %d\n", car3.brand, car3.model, car3.year);

  return 0;
}
Output

BMW X5 1999
Ford Mustang 1969
Toyota Corolla 2011