How are structures used in the C programming language?
In the C language, a structure is a user-defined data type that can contain multiple data members of different types. One can use the following methods to utilize structures:
- building or framework
struct Person {
char name[20];
int age;
float height;
};
- Declare a struct variable: use a defined struct type to declare a struct variable. For example:
struct Person p1;
- I want to improve my English skills.
strcpy(p1.name, "John");
p1.age = 25;
p1.height = 1.75;
- Structures can be passed as parameters in functions. For example:
void printPerson(struct Person p) {
printf("Name: %s\n", p.name);
printf("Age: %d\n", p.age);
printf("Height: %.2f\n", p.height);
}
- Structure pointers: You can use structure pointers to manipulate structures. For example:
struct Person *ptr;
ptr = &p1;
printf("Name: %s\n", ptr->name);
printf("Age: %d\n", ptr->age);
printf("Height: %.2f\n", ptr->height);
These are some fundamental ways to use structures that can help you in using structures in the C language.