While I was inputting "stuList[1].name", an error occurred and the program crashed. How do I fix this?
#include <stdio.h>
#include <conio.h>
typedef struct student
{
int id;
char *name;
float percentage;
} student;
int main()
{
student stuList[3];
stuList[0].name = "vermidonhapic";
stuList[2].name = "didiervermiyer";
scanf("%s\n",&stuList[1].name);
printf(" name is: %s \n", stuList[0].name);
printf(" name is: %s \n", stuList[2].name);
printf(" name is: %s \n", stuList[1].name);
system("PAUSE");
}
scanf()with a"%s"format is extremely dangerous, even if the pointer points to properly allocated memory. It will read an arbitrarily long string into the destination buffer; there's no way to prevent it from writing past the end of your array.