C program to store information using structures with dynamic memory allocation code example
Example: WAP to read and print ‘n’ student details using structure and Dynamic Memory Allocation.
/*C program to read and print the N student
details using structure and Dynamic Memory Allocation.*/
#include
#include
/*structure declaration*/
struct student
{
char name[30];
int roll;
float perc;
};
int main()
{
struct student *pstd;
int n,i;
printf("Enter total number of elements: ");
scanf("%d",&n);
/*Allocate memory dynamically for n objetcs*/
pstd=(struct student*)malloc(n*sizeof(struct student));
if(pstd==NULL)
{
printf("Insufficient Memory, Exiting... \n");
return 0;
}
/*read and print details*/
for(i=0; iname);
printf("Enter roll number: ");
scanf("%d",&(pstd+i)->roll);
printf("Enter percentage: ");
scanf("%f",&(pstd+i)->perc);
}
printf("\nEntered details are:\n");
for(i=0; iname,(pstd+i)->roll,(pstd+i)->perc);
}
return 0;
}