Originally Posted by
jimblumberg
So show me how you would create a normal (no dynamic memory involved) array of that structure.
Okay, so what did you try? .
Array of structure with no dynamic memory
Code:
#include<stdio.h>#include <string.h>
struct student{
int rollno;
char name[10];
};
int main()
{
int i;
struct student record[3];
printf("Enter Records of 3 students");
for(i=0;i<3;i++)
{
printf("\nEnter Rollno:");
scanf("%d",&record[i].rollno);
printf("\nEnter Name:");
scanf("%s",&record[i].name);
}
printf("\nStudent Information List:");
for(i=0;i<3;i++)
{
printf("\nRollno:%d, Name:%s",record[i].rollno,record[i].name);
}
return 0;
}
Array of structure with dynamic memory
Code:
#include<stdio.h>#include <string.h>
struct student{
int rollno;
char name[10];
};
int main()
{
struct student *ptr;
int i, Records = 3;
ptr = (struct student*) malloc (Records * sizeof(struct student));
for(i=0; i<record; i++)
{
printf("\nEnter Rollno:");
scanf("%d",&record[i].rollno);
printf("\nEnter Name:");
scanf("%s",&record[i].name);
}
printf("\nStudent Information List:");
for(i=0; i<record; i++)
{
printf("\nRollno:%d, Name:%s",record[i].rollno,record[i].name);
}
return 0;
}