C Pointer to Structure 🎯

beginner
21 min

C Pointer to Structure 🎯

Welcome to another exciting lesson at CodeYourCraft! Today, we're going to delve into the fascinating world of C Pointers to Structures. By the end of this lesson, you'll have a solid understanding of how to manipulate and manage structures using pointers, which is essential for working with complex data structures in C.

Let's start with the basics! 📝

Structures in C

In C, a structure is a user-defined data type that allows you to combine multiple data types into a single entity. Structures are extremely useful when dealing with complex data like records, objects, or any other set of related data items.

c
// Definition of a structure for a Student struct Student { int roll_number; char name[50]; float cgpa; };

Here, we've defined a structure Student with three fields: roll_number, name, and cgpa. Each field has its own data type.

Pointers to Structures 💡

Pointers to structures allow you to create variables that hold the address of a structure. This enables you to manipulate the contents of the structure using a single pointer.

c
// Declaration of a pointer to a Student structure struct Student *student_ptr; // Allocating memory for a Student structure student_ptr = (struct Student*) malloc(sizeof(struct Student)); // Initializing the structure using the pointer student_ptr->roll_number = 1; strcpy(student_ptr->name, "John Doe"); student_ptr->cgpa = 3.5;

In the example above, we've declared a pointer to a Student structure called student_ptr. We then allocate memory for a Student structure using malloc() and initialize the structure using the pointer.

Dynamic Allocation of Structures ✅

To make our code more flexible, we can dynamically allocate an array of structures using malloc().

c
// Dynamic allocation of an array of Student structures struct Student *students; int num_students; // Allocate memory for the number of students entered by the user students = (struct Student*) malloc(num_students * sizeof(struct Student)); // Input the number of students printf("Enter the number of students: "); scanf("%d", &num_students); // Input the details of each student for (int i = 0; i < num_students; i++) { printf("\nEnter details for student %d:\n", i + 1); printf("Roll number: "); scanf("%d", &students[i].roll_number); printf("Name: "); scanf("%s", students[i].name); printf("CGPA: "); scanf("%f", &students[i].cgpa); } // Free the memory allocated for the Student structures free(students);

In this example, we've dynamically allocated an array of Student structures, inputted the details of each student, and then freed the memory after use.

Quiz 📝

That's it for today! We've covered the basics of using pointers with structures in C. In the next lesson, we'll dive deeper into manipulating structures and arrays of structures using pointers. Stay tuned! 🚀

Remember to practice, practice, practice! The more you code, the better you'll understand these concepts. Happy learning, and see you in the next lesson! 👋