In C language, a structure is a user-defined data type that groups together variables of different data types under a single name. A structure is similar to a record or a class in other programming languages.
A structure declaration defines a blueprint for the structure, specifying the name of the structure and the member variables that it contains. Each member variable can be of a different data type, such as int, float, char, or even another structure.
Here’s an example of a structure declaration in C:
struct person {
char name[50];
int age;
float height;
};
This defines a structure called “person” that contains three member variables: a character array named “name” with a maximum size of 50 characters, an integer named “age”, and a floating-point number named “height”.
Once a structure is defined, it can be used to declare variables of that type, just like any other data type in C:
struct person john; // Declare a variable of type "person" named "john"
You can then access the member variables of the structure using the dot operator (.):
strcpy(john.name, "John Smith"); // Assign a value to the "name" member variable
john.age = 30; // Assign a value to the "age" member variable
john.height = 1.75; // Assign a value to the "height" member variable