As I try compiling my c code it keeps notifying me that " line 13:error:identifier expected ".my programming involves storing string of arrays -


#include<stdio.h>  struct names  { char name1[49];  char name2[48];  char name3[49];  };  stud1,stud2,stud3;  int main()  {  char names;  printf("enter first name of first student\n");   scanf("%s",&struct names.name1.stud1);  printf("enter second  name of first student\n");  scanf("%s",&struct names.name2.stud1);  printf("enter third  name of first student\n");  scanf("%s",&struct names.name3.stud1);  printf("enter first name of second  student\n");  scanf("%s",&struct names.name1.stud2);  printf("enter second  name of second  student\n");  scanf("%s",&struct names.name2.stud2);  printf("enter third  name of second  student\n");  scanf("%s",&struct names.name3.stud2);  printf("enter first name of third  student\n");  scanf("%s",&struct names.name1.stud3);  printf("enter second  name of third student\n");  scanf("%s",&struct names.name2.stud3);  printf("enter third  name of third student\n");  scanf("%s",&struct names.name3.stud3);  return 0; } 

your struct definition looks okay, this:

stud1,stud2,stud3; 

will declare 3 integer variables. won't compile under newer standards such c99. want 3 variable of type struct names here:

struct names stud1, stud2, stud3; 

when use variables:

scanf("%s",&struct names.name1.stud1); 

you don't use struct names, type of variable stud1. used when declate variable compiler knows type is. after that, use name. also, struct variable bname goes first, field name next. , fiels name1 char array, shouldn't take address. finally, idea signal scanf not overflow array. so:

scanf("%48s", &stud1.name1); 

please read chapter on types , variables in c primer again.

addendum: okay define struct , variables in 1 go, others have suggested:

struct names {     char name1[49];     char name2[48];     char name3[49]; } stud1, stud2, stud3; 

i find such compound definitions more confusing useful. recommend beginner, should stick separation of struct , variable definition.

you can definition removing seimcolon after struct body in code, not fix systax use structs.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -