I'm writing a program in c that looks through a standardly inputted text file with lines "string int". I had the program running fine with scanf originally, but now the name has to be a dynamic string(size known at runtime) and when I switched the name in grade_entry to a pointer it started segmentation faulting
typedef struct grade_entry {
char *name;
int grade;
} grade_entry;
struct grade_entry grade_list[100];
int main(){
int grade;
int done;
int i=0;
do{
puts("not weee\n");
done=(int)strlen(gets(grade_list[i].name));
puts("weee\n");
}while(1);
}
compiles and current output is:
not weee
segmentation fault(core dumped)
**Resolved: I allocated the pointer and then reallocated for size+1, adding the null to The end. It's unfortunate that I can't malloc immediately based off of the temp memory for scanf,fgets,gets. Oh well, thank you everyone for the help
You have to allocate memory to grade_entry.name
, otherwise it is a char pointer pointing to an arbitrary memory location and writing to it is causing segmentation fault.