In C, a structure is a user-defined data type that allows grouping of variables of different data types under a single name. This is useful when you need to represent a collection of related data as a single entity, such as information about a person, a student, a book, etc. Structures provide a way to organize and manage complex data more efficiently.
Let’s explore the concept of structures in C, how to define and access them, and how to use them with functions.
A structure is defined using the struct keyword followed by the structure name and a block containing the structure members. The members can be of different data types.
struct structure_name {
data_type member1;
data_type member2;
// Additional members
};
#include <stdio.h>
// Defining a structure to represent a student
struct Student {
char name[50]; // Name of the student
int age; // Age of the student
float grade; // Grade of the student
};
int main() {
// Declare a structure variable of type Student
struct Student student1;
// Assigning values to the members of student1
strcpy(student1.name, "John Doe");
student1.age = 20;
student1.grade = 85.5;
// Printing the values of student1
printf("Student Name: %s\n", student1.name);
printf("Student Age: %d\n", student1.age);
printf("Student Grade: %.2f\n", student1.grade);
return 0;
}
struct Student defines a structure with three members: name, age, and grade.student1 is created as an instance of the structure Student.strcpy() function is used to assign a string to the name member because arrays of characters (strings) cannot be directly assigned.Student Name: John Doe
Student Age: 20
Student Grade: 85.50
Structure members are accessed using the dot operator (.). After declaring a structure variable, you can use this operator to access and modify its members.
#include <stdio.h>
struct Point {
int x;
int y;
};
int main() {
struct Point point1; // Declare a structure variable
// Accessing structure members and assigning values
point1.x = 10;
point1.y = 20;
// Accessing structure members and printing them
printf("Point coordinates: (%d, %d)\n", point1.x, point1.y);
return 0;
}
point1.x and point1.y access the x and y members of the structure point1, and we assign values to these members.printf().Point coordinates: (10, 20)
You can pass structures to functions in C, either by value (copying the structure data) or by reference (using pointers to the structure). Let's see how structures are used in functions.
When a structure is passed to a function by value, the function gets a copy of the structure, meaning that changes made inside the function do not affect the original structure.
#include <stdio.h>
struct Student {
char name[50];
int age;
float grade;
};
// Function to print student details (passing by value)
void printStudent(struct Student s) {
printf("Student Name: %s\n", s.name);
printf("Student Age: %d\n", s.age);
printf("Student Grade: %.2f\n", s.grade);
}
int main() {
struct Student student1 = {"Alice", 22, 90.5};
// Passing structure to function by value
printStudent(student1);
return 0;
}
Student is passed to the printStudent() function by value.Student Name: Alice
Student Age: 22
Student Grade: 90.50
When a structure is passed by reference, the function works with the original structure. Any changes made inside the function will affect the original structure.
#include <stdio.h>
struct Student {
char name[50];
int age;
float grade;
};
// Function to modify student details (passing by reference)
void updateStudent(struct Student *s, const char *newName, int newAge, float newGrade) {
// Modifying structure members using pointer
strcpy(s->name, newName);
s->age = newAge;
s->grade = newGrade;
}
int main() {
struct Student student1 = {"Alice", 22, 90.5};
printf("Before Update:\n");
printf("Student Name: %s\n", student1.name);
printf("Student Age: %d\n", student1.age);
printf("Student Grade: %.2f\n", student1.grade);
// Passing structure to function by reference (using pointer)
updateStudent(&student1, "Bob", 25, 95.0);
printf("\nAfter Update:\n");
printf("Student Name: %s\n", student1.name);
printf("Student Age: %d\n", student1.age);
printf("Student Grade: %.2f\n", student1.grade);
return 0;
}
updateStudent() takes a pointer to a Student structure as an argument, which allows it to modify the original structure.s->name, s->age, and s->grade access the members of the structure through the pointer.student1 is modified by the function because the address of student1 is passed.Before Update:
Student Name: Alice
Student Age: 22
Student Grade: 90.50
After Update:
Student Name: Bob
Student Age: 25
Student Grade: 95.00
C functions can return structures, which allows functions to generate and return structured data.
#include <stdio.h>
struct Student {
char name[50];
int age;
float grade;
};
// Function to create and return a student
struct Student createStudent(const char *name, int age, float grade) {
struct Student s;
strcpy(s.name, name);
s.age = age;
s.grade = grade;
return s;
}
int main() {
// Creating a student by calling the function
struct Student student1 = createStudent("Charlie", 20, 88.5);
printf("Student Name: %s\n", student1.name);
printf("Student Age: %d\n", student1.age);
printf("Student Grade: %.2f\n", student1.grade);
return 0;
}
createStudent() function returns a Student structure by creating a structure, assigning values, and returning it.student1, and its members are printed.Student Name: Charlie
Student Age: 20
Student Grade: 88.50
struct keyword, and they can hold members of different data types..).Using structures in C allows you to group related data, making your programs more organized and easier to manage.
Open this section to load past papers