1. Introduction to C Structures (structs)
C structures, commonly referred to as structs, are user-defined data types that allow you to group related data items together into a single unit. A struct can contain multiple members of different data types, such as integers, floating-point numbers, characters, or even other structures. This makes structs a powerful tool for organizing and manipulating data in C programs.
2. Creating a Structure
To create a struct, you need to define its layout and members. The syntax for creating a struct is as follows:
struct structure_name {
data_type member1;
data_type member2;
// ...
};
Here's an example of defining a struct called Person with members for name, age, and height:
struct Person {
char name[50];
int age;
float height;
};
3. Accessing Structure Members
Once you've defined a struct, you can create variables of that struct type and access its members using the dot (.) operator.
Here's an example:
struct Person person1;
strcpy(person1.name, "John Doe");
person1.age = 25;
person1.height = 1.75;
In this example, we create a variable person1 of type struct Person and assign values to its members (name, age, and height) using the dot operator.
4. Strings in Structures
Structs can also contain members of type char arrays, allowing you to store and manipulate strings within structures.
Here's an example:
struct Person {
char name[50];
int age;
};
struct Person person1;
strcpy(person1.name, "John Doe");
person1.age = 25;
In this case, the name member of the Person struct is an array of characters, which can store a string of up to 49 characters (plus the null terminator).
5. Simpler Syntax (Optional)
C99 introduced a simpler syntax for defining and initializing structs, known as designated initializers. It allows you to initialize specific members of a struct without specifying them in order.
Here's an example:
struct Person {
char name[50];
int age;
float height;
};
struct Person person1 = {
.age = 25,
.name = "John Doe",
.height = 1.75
};
Using designated initializers, you can specify the values of struct members explicitly by name, making the code more readable.
6. Copying Structures
To copy the values of one struct into another, you can simply use the assignment operator (=). This will copy each member from the source struct to the destination struct.
Here's an example:
struct Person person1;
struct Person person2;
strcpy(person1.name, "John Doe");
person1.age = 25;
person1.height = 1.75;
person2 = person1; // Copying person1 to person2
After the assignment, person2 will have the same values as person1.
7. Modifying Structure Values
You can modify the values of struct members directly by assigning new values to them.
Here's an example:
struct Person person1;
strcpy(person1.name, "John Doe");
person1.age = 25;
person1.height = 1.75;
person1.age = 26; // Modifying the age member
In this case, we change the value of the age member in person1 from 25 to 26.
That covers the basics of working with C structures (structs). By creating structures, accessing their members, using strings within structures, employing simpler syntax, copying structures, and modifying values, you can effectively organize and manipulate data in your C programs.