Structures in C Language:
Structures in C
Structures in C are user-defined data types that group related variables of different data types. They are useful for representing a record or a composite data type.
Declaring and Using Structures
#include
// Define a structure
struct Person {
char name[50];
int age;
float height;
};
int main() {
// Declare a structure variable
struct Person person1;
// Initialize structure members
strcpy(person1.name, "John Doe");
person1.age = 30;
person1.height = 5.9;
// Access and print structure members
printf("Name: %s\n", person1.name);
printf("Age: %d\n", person1.age);
printf("Height: %.1f\n", person1.height);
return 0;
}
Accessing Structure Members Using Pointers
You can access structure members using pointers to structures. The arrow operator (->
) is used to access members through a pointer.
#include
#include
// Define a structure
struct Person {
char name[50];
int age;
float height;
};
int main() {
// Declare a structure variable
struct Person person1;
// Initialize structure members
strcpy(person1.name, "John Doe");
person1.age = 30;
person1.height = 5.9;
// Declare a pointer to the structure
struct Person *ptr;
// Assign the address of person1 to the pointer
ptr = &person1;
// Access and print structure members using the pointer
printf("Name: %s\n", ptr->name);
printf("Age: %d\n", ptr->age);
printf("Height: %.1f\n", ptr->height);
return 0;
}
Nested Structures
Structures can be nested, meaning a structure can contain another structure as a member.
#include
#include
// Define a structure for address
struct Address {
char city[50];
char state[50];
int zip;
};
// Define a structure for person that includes address
struct Person {
char name[50];
int age;
float height;
struct Address address;
};
int main() {
// Declare and initialize a structure variable
struct Person person1;
strcpy(person1.name, "John Doe");
person1.age = 30;
person1.height = 5.9;
strcpy(person1.address.city, "New York");
strcpy(person1.address.state, "NY");
person1.address.zip = 10001;
// Access and print nested structure members
printf("Name: %s\n", person1.name);
printf("Age: %d\n", person1.age);
printf("Height: %.1f\n", person1.height);
printf("City: %s\n", person1.address.city);
printf("State: %s\n", person1.address.state);
printf("ZIP: %d\n", person1.address.zip);
return 0;
}
Array of Structures
You can create an array of structures to store multiple records of the same type.
#include
#include
// Define a structure
struct Person {
char name[50];
int age;
float height;
};
int main() {
// Declare an array of structures
struct Person people[2];
// Initialize the array
strcpy(people[0].name, "John Doe");
people[0].age = 30;
people[0].height = 5.9;
strcpy(people[1].name, "Jane Smith");
people[1].age = 25;
people[1].height = 5.5;
// Access and print array of structures
for (int i = 0; i < 2; i++) {
printf("Person %d:\n", i + 1);
printf(" Name: %s\n", people[i].name);
printf(" Age: %d\n", people[i].age);
printf(" Height: %.1f\n", people[i].height);
}
return 0;
}
Function Arguments
Structures can be passed to functions by value or by reference using pointers.
Passing by Value
#include
#include
// Define a structure
struct Person {
char name[50];
int age;
float height;
};
void printPerson(struct Person p) {
printf("Name: %s\n", p.name);
printf("Age: %d\n", p.age);
printf("Height: %.1f\n", p.height);
}
int main() {
struct Person person1;
strcpy(person1.name, "John Doe");
person1.age = 30;
person1.height = 5.9;
printPerson(person1);
return 0;
}
Passing by Reference
#include
#include
// Define a structure
struct Person {
char name[50];
int age;
float height;
};
void printPerson(struct Person *p) {
printf("Name: %s\n", p->name);
printf("Age: %d\n", p->age);
printf("Height: %.1f\n", p->height);
}
int main() {
struct Person person1;
strcpy(person1.name, "John Doe");
person1.age = 30;
person1.height = 5.9;
printPerson(&person1);
return 0;
}