C Structures
What is a Structure in C?
A structure in C, declared using the struct keyword, is a user-defined type that bundles together multiple related variables — even if they differ in data types — under a single composite unit.
Each field in a structure is known as a member, and it can be an integer, float, character array, or any other type.
This mechanism is incredibly useful for grouping logically related data, such as attributes of a student, employee, or vehicle.
Defining a Structure
To define a structure in C, begin with the struct keyword, assign it an identifier, then enclose its attributes within braces {}, finishing with a semicolon to complete the statement.
Example:
struct Profile {
int id;
char grade;
}; This creates a new structure type named Profile with two fields: id (integer) and grade (character).
Instantiating a Structure
Once the structure layout is established, you can generate variables from it just like using a custom data type.
struct Profile p1;
Here, p1 is a variable of type struct Profile.
You can declare structure instances inside main() or globally, depending on the scope required.
Using Structure Members
To interact with a structure’s fields, apply the dot (.) operator between the variable and the desired member.
Example:
struct Profile {
int id;
char grade;
};
int main() {
struct Profile student;
student.id = 101;
student.grade = 'A';
printf("ID: %d\n", student.id);
printf("Grade: %c\n", student.grade);
Return 0;
} Multiple Instances
Structures allow you to define multiple independent variables of the same layout:
struct Profile p1 = {101, 'A'};
Struct Profile p2 = {102, 'B'}; Every instance of a struct can contain its own distinct data while adhering to the same field blueprint.
Handling Strings in Structs
In C, strings are essentially character arrays, and assigning a string literal to an array directly using = is not allowed.
Invalid:
s1.name = "John";
Instead, use the strcpy() function from
Correct:
#include <string.h> Strcpy(s1.name, "John"); // ✔️ Right
String Support Example
struct Profile {
int id;
char grade;
char name[30];
};
int main() {
struct Profile s1;
strcpy(s1.name, "John");
printf("Name: %s", s1.name);
return 0;
} Initializing at Declaration
Values can be directly set to struct members at the time of declaration using a comma-separated initializer enclosed in braces.
struct Profile {
int id;
char grade;
char name[30];
};
Struct Profile s1 = {101, 'A', "Alice"}; Note: The values must appear in the same order as the member declarations.
Structure Assignment (Copying)
A structure's contents can be duplicated into another variable of identical type through simple assignment.
struct Profile s1 = {101, 'A', "Alice"};
struct Profile s2;
s2 = s1; This duplicates all field values from s1 to s2.
Updating Structure Values
To update values within a struct, apply the dot (.) notation to target and change specific fields.
s1.id = 200; s1.grade = 'B'; Strcpy(s1.name, "Bob");
This approach also works after copying from another structure, allowing you to update each field as needed.
Real-World Scenario: Cars Database
Let’s say you want to store different properties of multiple vehicles. Structures help keep this info together cleanly.
struct Car {
char brand[50];
char model[50];
int year;
};
int main() {
struct Car car1 = {"Honda", "Civic", 2020};
struct Car car2 = {"Tesla", "Model S", 2022};
struct Car car3 = {"Audi", "Q7", 2018};
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;
} This showcases how structures help in organizing data across multiple instances efficiently and readably.
Key Takeaways
| Concept | Description |
|---|---|
| struct keyword | Used to define a structure type |
| Dot notation (.) | Accesses individual members of a structure |
| String handling | Use strcpy() for assigning character arrays inside structs |
| Initialization | Struct fields can be populated right when the variable is defined using a value list. |
| Copying structures | Use the = operator to replicate all data from one struct to another of the same type. |
| Field updates | 4o |
| Use case | Ideal for grouping related data like employee records, cars, students |
Prefer Learning by Watching?
Watch these YouTube tutorials to understand C Tutorial visually:
What You'll Learn:
- 📌 Introduction to Structures in C
- 📌 Structures in C Programming | Introduction to Structures in C | C Language Tutorial | Simplilearn