Curriculum
Course: C basic
Login

Curriculum

C basic

C Introduction

0/1

C Get Started

0/1

C Comments

0/1

C Constants

0/1

C Operators

0/1

C Break and Continue

0/1

C User Input

0/1

C Memory Address

0/1

C Structures

0/1
Text lesson

C Structures

Structures

Structures, or structs, serve as a method for consolidating multiple related variables into a single entity. Each variable within the structure is referred to as a member of the structure.

In contrast to an array, a structure can accommodate various data types such as integers, floats, characters, and more.

Create a Structure

To create a structure, you utilize the “struct” keyword and define each member within curly braces.

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

To access the structure, you need to instantiate a variable of that structure type.

Inside the main() method, use the struct keyword followed by the structure name, and then declare the structure variable.

Instantiate a struct variable named “s1”.

struct myStructure {
  int myNum;
  char myLetter;
};

int main() {
  struct myStructure s1;
  return 0;

Access Structure Members

To access members of a structure, employ the dot syntax ( . ):

Example

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

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

  // 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;

Now, you can effortlessly generate multiple structure variables with distinct values, utilizing just one 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?

Keep in mind that in C, strings are essentially arrays of characters, and regrettably, you cannot assign a value directly to an array in this manner.

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”;

  // Trying to print the value
  printf(“My string: %s”, s1.myString);

  return 0;

An error will be triggered.

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

Fortunately, there is a solution for this! You can utilize the strcpy() function to assign the value to s1.myString, as shown below:

Example

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

int main() {
  struct myStructure s1;

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

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

  return 0;

Result:

My string: Some text

Simpler Syntax

You can also assign values to members of a structure variable during declaration, all in a single line.

Simply include the values in a comma-separated list within curly braces {}. It’s important to note that you don’t need to use the strcpy() function for string values when using this technique.

Example

// Create a structure
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;

 

Note that the sequence of inserted values must correspond to the order of variable types declared in the structure (13 for int, ‘B’ for char, etc.).

Copy Structures

Another option is to assign one structure to another.

In the following example, the values of s1 are copied into s2:

Example

struct myStructure s1 = {13, ‘B’, “Some text”};
struct myStructure s2;

s2 = s1;

Modify Values

To change or modify a value, you can utilize the dot syntax (.).

Additionally, when modifying a string value, the strcpy() function proves useful once more:

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”};

  // 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;

Adjusting values becomes particularly useful when duplicating 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); 

 

Ok, so, how are structures useful?

Consider a scenario where you need to develop a program to store various details about cars, like their brand, model, and year. The beauty of structures lies in the ability to create a unified “Car template” that can be applied to every car you define. Below is a real-life example to illustrate this concept.

Real-Life Example

Utilize a structure to store diverse details about cars.

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;
}