Please help me with this code. Why same name is assigned to all the different structs? I'm noob to pointers and arrays in C.
struct student
{
char *name;
int reg_no;
};
typedef struct student student;
int main()
{
char choice = 'y';
char name[30];
int rg;
student students[5];
int n = 0;
while(choice != 'n')
{
printf("enter student's name\n>>");
gets(name);
printf("enter student's reg_no\n>>");
scanf("%d",&rg);
getchar();
students[n].name = name;
students[n].reg_no = rg;
printf("enter details of more students (y/n)\n>>");
scanf("%c",&choice);
getchar();
n++;
}
for(int i=0;i<n;i++)
{
printf("Details of student%d :\n",(i+1));
printf("Name : %s\n",students[i].name );
printf("Reg no. : %d\n\n",students[i].reg_no );
}
return 0;
}
Running in console :
Edit : Added student struct