1
#include <stdio.h>

struct virus
{
    char signature[25];
    int size;
}v[2];

int main(void) {

    static v[0] = {"Yankee",1813};
    static v[1] = {"Doodle",2813};
    int i;

    for(i=0;i<=1;i++)
    {
        printf("%s %d\n",v[i].signature,v[i].size);
    }

    return 0;
}

I am getting the compiler error in this C code.

Error: Declaration syntax in function main()

I am guessing that there is some error in v[2], as it is associated with extern class whereas, v[0] and v[1] are associated with static class.

But, I am not sure that is this the only reason or some other ?


Edit : I have edited the code by removing the wrong syntax.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261

2 Answers2

4

There is no error in declaration of v[2], the problem is later.

You've written

 static struct v[0] = {"Yankee",1813};

which attempts to define a 0-sized array, which is not allowed by default C standard.

That said, the syntax is also horribly wrong. You don't have a proper type there, remember, struct itself is not a type, it's a keyword. struct <something> is actually a type.

Then, from the logical point of view, you probably don't want a new variable altogether. In case you want to use the array elements from the v, just use the variable name, that's all. Something like

#include <stdio.h>

struct virus
{
    char signature[25];
    int size;
}v[2] = { {"Yankee",1813}, {"Doodle",2813}}; //get it initialized, job done

int main(void) {

    int i;
    for(i=0;i<=1;i++)
    {
        printf("%s %d\n",v[i].signature,v[i].size);
    }

    return 0;
}

will do the job in much better way, IMHO.


EDIT:

In case, you're interested in assigning individual elements (not initialization), well, you cannot use a brace-enclosed initializer for that purpose, it's not meant to be RHS operand for an assignment. You need to use a compound literal for that purpose, something like

v[0] = (struct virus){"Yankee",1813};
v[1] = (struct virus){"Doodle",2813};

will also do the job.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • v[0] = {"Yankee",1813}; But why this is not working also ? –  Mar 03 '17 at 06:38
  • If I only write v[0] = {"Yankee",1813} . It's still not working. Can you provide some reason based on that . Besides, I got your way in your answer. –  Mar 03 '17 at 06:45
  • @SouravGhosh Thanks :)) Learn't something new. –  Mar 03 '17 at 07:09
  • 1
    @Barry you're welcome, that's the whole point of being here, inn'it? Happy learning. :) – Sourav Ghosh Mar 03 '17 at 07:10
  • @SouravGhosh If you don't mind, Can you give me some reference for such methodology used in particularly structures !! –  Mar 03 '17 at 08:06
  • 1
    @Barry What sort of reference? [did you see this](http://stackoverflow.com/q/562303/2173917)? – Sourav Ghosh Mar 03 '17 at 08:08
  • @SouravGhosh Thanks !! That suffices . –  Mar 03 '17 at 08:16
0

Don't mix up struct definitions with variable declarations, that's sloppy practice.

Instead, use a typedef:

typedef struct 
{
    char signature[25];
    int size;
} virus_t;

Then you can declare variables of this type as you please:

static virus_t v[2] = 
{
  {"Yankee",1813},
  {"Doodle",2813}
};

Or with designated initializers:

static virus_t v[2] = 
{
  [0] = {"Yankee",1813},
  [1] = {"Doodle",2813}
};
Lundin
  • 195,001
  • 40
  • 254
  • 396