```markdown
In C programming, a structure (often called a struct
) is a user-defined data type that groups together different types of variables under a single name. These variables, known as members or fields, can be of different data types. Structures allow you to model more complex data and are essential in organizing data in a logical manner.
A structure is defined using the struct
keyword followed by the name of the structure and its members. Here's the basic syntax:
c
struct StructureName {
data_type member1;
data_type member2;
data_type member3;
// Add more members as needed
};
c
struct Person {
char name[50];
int age;
float height;
};
In this example, we define a structure Person
that contains three members: name
(a string), age
(an integer), and height
(a floating-point number).
Once a structure is defined, you can declare variables of that structure type. You can declare one or more variables of the structure type in the same statement.
c
struct Person person1, person2;
This creates two variables, person1
and person2
, both of type struct Person
.
Structure members are accessed using the dot (.
) operator. Here's how you can access and assign values to the members of a structure:
```c struct Person person1;
strcpy(person1.name, "John Doe"); // Assigning a string to the 'name' member person1.age = 30; // Assigning an integer to the 'age' member person1.height = 5.9; // Assigning a float to the 'height' member ```
In this example, the strcpy()
function is used to assign a string to the name
member (since name
is an array), and the other members are assigned directly.
Structures can also be initialized when they are declared. This can be done by providing values in the same order as the structure members.
c
struct Person person1 = {"John Doe", 30, 5.9};
This initializes person1
with the name
as "John Doe", age
as 30, and height
as 5.9.
Just like other variables, pointers can be used to reference structures. When using a pointer to a structure, the arrow (->
) operator is used to access members.
```c struct Person person1; struct Person *ptr = &person1
ptr->age = 25; strcpy(ptr->name, "Jane Doe"); ptr->height = 5.6; ```
In this example, the pointer ptr
is used to access and modify the structure's members using the arrow (->
) operator.
Structures can also contain other structures as members. This is useful when you need to represent more complex data models.
```c struct Address { char street[100]; char city[50]; char state[50]; int zipCode; };
struct Person { char name[50]; int age; struct Address address; // Nested structure }; ```
Here, Person
contains an Address
structure as a member. You can access the nested structure's members using the dot operator.
```c struct Person person1;
strcpy(person1.name, "John Doe"); person1.age = 30; strcpy(person1.address.street, "123 Main St"); strcpy(person1.address.city, "Springfield"); person1.address.zipCode = 12345; ```
Structures are a fundamental concept in C programming. They help organize data in a way that makes it easier to manage and manipulate. By combining different data types into one unit, structures provide a powerful way to represent complex real-world entities in your programs. ```