Structures in C are user-defined data types that allow you to group variables of different data types together under a single name. They are helpful in organizing complex data, especially when dealing with records or entities that consist of multiple attributes.

Defining a Structure

To define a structure, you use the struct keyword, followed by the structure name and a block containing the members (variables) of the structure:

struct Person { char name[50]; int age; float height; };

Here:

  • Person is the name of the structure.
  • It contains three members: name, age, and height.
  • name is an array of characters, age is an integer, and height is a floating-point number.

Declaring Structure Variables

After defining a structure, you can declare variables of that structure type:

struct Person person1, person2;

Alternatively, you can combine the definition and declaration:

struct Person { char name[50]; int age; float height; } person1, person2;

Initializing Structure Members

You can initialize a structure variable in the following ways:

struct Person person1 = {"Alice", 25, 5.7};

You can also assign values to individual members using the dot (.) operator:

struct Person person2; strcpy(person2.name, "Bob"); person2.age = 30; person2.height = 6.1;

Accessing Structure Members

To access members of a structure, you use the dot operator:

printf("Name: %s\n", person1.name); printf("Age: %d\n", person1.age); printf("Height: %.1f\n", person1.height);

Structure in Functions

Structures can be used as arguments to functions, allowing you to pass entire records at once.

Passing Structure by Value

When you pass a structure by value, a copy of the structure is passed:

void display(struct Person p) { printf("Name: %s, Age: %d, Height: %.1f\n", p.name, p.age, p.height); } int main() { struct Person person1 = {"Alice", 25, 5.7}; display(person1); return 0; }

Passing Structure by Reference

Passing a structure by reference is more memory efficient, as you pass a pointer to the structure rather than copying it:

void modify(struct Person *p) { p->age += 1; // Access members using the arrow (->) operator } int main() { struct Person person1 = {"Alice", 25, 5.7}; modify(&person1); printf("Age after modification: %d\n", person1.age); // Output: 26 return 0; }

Arrays of Structures

You can create arrays of structures, which is useful for storing multiple records:

struct Person people[3] = { {"Alice", 25, 5.7}, {"Bob", 30, 6.1}, {"Charlie", 28, 5.9} }; for (int i = 0; i < 3; i++) { printf("Name: %s, Age: %d, Height: %.1f\n", people[i].name, people[i].age, people[i].height); }

Nested Structures

You can also have structures within structures, which is called nesting:

struct Address { char street[50]; char city[50]; int zipCode; }; struct Person { char name[50]; int age; struct Address address; // Nested structure }; int main() { struct Person person = {"Alice", 25, {"Main St", "New York", 10001}}; printf("Name: %s, City: %s, Zip: %d\n", person.name, person.address.city, person.address.zipCode); return 0; }

typedef with Structures

Using typedef allows you to create an alias for a structure, making the code cleaner:

typedef struct { char name[50]; int age; float height; } Person; Person person1 = {"Alice", 25, 5.7};

This way, you do not need to use the struct keyword every time you declare a variable of that type.

Memory Layout

In a structure, each member occupies its own space in memory, and the total size of a structure is the sum of the sizes of its members (possibly with padding). Padding is added to align data in memory for more efficient access.

For example:

struct Example { char c; // 1 byte int i; // 4 bytes double d; // 8 bytes };

Here, the total memory occupied might be larger than 13 bytes due to alignment and padding to ensure efficient access.

Summary of Structures

  • Structures are used to group related data of different types.
  • Use the struct keyword to define a structure, and the dot (.) operator to access members.
  • Structures can be passed to functions, either by value or by reference.
  • They can be nested or stored in arrays.
  • Using typedef with structures allows for simpler syntax when declaring structure variables.

Structures are fundamental in C for building complex data models, such as representing entities with multiple properties, and they are the basis for creating more sophisticated data types.