1

I am trying to declare a structure outside main function. I was able to declare its variables outside the main function but when i try to initialize them the way shown below:

struct test
{
int a;
};
struct test abc;
abc.a=1;
int main()
{
printf("%d\n",abc.a);
return 0;
}

I am getting error error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token. When i initialize the structure variables at the time I declared them, everything goes smooth but i am getting an error when using the way above. Why is it happening and what does this error mean?

LocalHost
  • 910
  • 3
  • 8
  • 24
  • 3
    You can't use statements outside of a function. That includes assignments. You *can* include an initializer with a declaration. In your case, you want `struct test abc = {1};` That's an initializer. – Tom Karzes Apr 29 '20 at 21:07
  • https://stackoverflow.com/questions/27664514/structure-member-assignment-causing-syntax-error-when-not-inside-a-function – Arkadiusz Drabczyk Apr 29 '20 at 21:09
  • 2
    Does this answer your question? [Structure member assignment causing syntax error when not inside a function](https://stackoverflow.com/questions/27664514/structure-member-assignment-causing-syntax-error-when-not-inside-a-function) – Hitokiri Apr 29 '20 at 21:11
  • @Hitokiri yes it answers my quest but partially. i am still not able to understand what this error is trying to tell. i understood the concept. Can you explain about this error ? – LocalHost Apr 30 '20 at 12:07

0 Answers0