Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Explain the dynamic memory allocation of pointer to structure in C language
Dynamic memory allocation of pointer to structure in C allows us to allocate memory for structures at runtime using functions like malloc(), calloc(), or realloc(). This approach is essential when the number of structures needed is not known at compile time.
Pointer to structure holds the address of the entire structure and is used to create complex data structures such as linked lists, trees, and graphs. The members of the structure can be accessed using the arrow operator (->).
Syntax
struct tagname *ptr; ptr = (struct tagname*) malloc(n * sizeof(struct tagname));
Declaration and Access
Following is the declaration for pointers to structures −
struct student *s;
Accessing structure members using pointer −
ptr->membername; // Example: s->sno, s->sname, s->marks
Example 1: Basic Dynamic Allocation
Following is a C program that demonstrates dynamic memory allocation for structures −
#include <stdio.h>
#include <stdlib.h>
struct person {
int age;
float weight;
char name[30];
};
int main() {
struct person *ptr;
int i, n;
printf("Enter the number of persons: ");
scanf("%d", &n);
// Allocating memory for n structures
ptr = (struct person*) malloc(n * sizeof(struct person));
if (ptr == NULL) {
printf("Memory allocation failed!<br>");
return 1;
}
for (i = 0; i < n; i++) {
printf("Enter name and age for person %d: ", i + 1);
scanf("%s %d", (ptr + i)->name, &(ptr + i)->age);
printf("Enter weight: ");
scanf("%f", &(ptr + i)->weight);
}
printf("\nDisplaying Information:<br>");
for (i = 0; i < n; i++) {
printf("Person %d - Name: %s, Age: %d, Weight: %.2f<br>",
i + 1, (ptr + i)->name, (ptr + i)->age, (ptr + i)->weight);
}
free(ptr); // Free allocated memory
return 0;
}
Enter the number of persons: 2 Enter name and age for person 1: Alice 25 Enter weight: 55.5 Enter name and age for person 2: Bob 30 Enter weight: 70.2 Displaying Information: Person 1 - Name: Alice, Age: 25, Weight: 55.50 Person 2 - Name: Bob, Age: 30, Weight: 70.20
Example 2: Using calloc() for Initialization
Using calloc() automatically initializes allocated memory to zero −
#include <stdio.h>
#include <stdlib.h>
struct student {
int id;
char name[20];
float marks;
};
int main() {
struct student *students;
int n = 3, i;
// Allocate and initialize memory to zero
students = (struct student*) calloc(n, sizeof(struct student));
if (students == NULL) {
printf("Memory allocation failed!<br>");
return 1;
}
// Input data
for (i = 0; i < n; i++) {
printf("Enter ID, name and marks for student %d: ", i + 1);
scanf("%d %s %f", &students[i].id, students[i].name, &students[i].marks);
}
// Display data
printf("\nStudent Records:<br>");
for (i = 0; i < n; i++) {
printf("ID: %d, Name: %s, Marks: %.2f<br>",
students[i].id, students[i].name, students[i].marks);
}
free(students);
return 0;
}
Enter ID, name and marks for student 1: 101 John 85.5 Enter ID, name and marks for student 2: 102 Mary 92.0 Enter ID, name and marks for student 3: 103 David 78.5 Student Records: ID: 101, Name: John, Marks: 85.50 ID: 102, Name: Mary, Marks: 92.00 ID: 103, Name: David, Marks: 78.50
Key Points
- malloc() allocates uninitialized memory, while calloc() initializes memory to zero.
- Always check if memory allocation was successful by verifying the returned pointer is not NULL.
- Use
free()to deallocate memory and prevent memory leaks. - Array notation
ptr[i]and pointer arithmetic(ptr+i)->memberare equivalent.
Conclusion
Dynamic memory allocation for structures provides flexibility in managing memory at runtime. Always remember to check for allocation success and free the allocated memory to avoid memory leaks in your programs.
