简体   繁体   中英

How to initialize a single element from an array of structures in C?

I am working with a code which reads many files and stores the count of keywords in it. A part of the code is as follows:

struct files
{
    struct keyword
    {
        char keyname[10];
        int count;
    }key[32];            //for 32 keywords in C
}file[10];    

How can I initialize the keyword structure for all the 10 files as {"void",0,"int",0,.....etc} ? Is there a method by which I can initialize all the 10 files in a loop by initializing each structure element at a time?

10 files can be initialized using a loop as shown below.

 for(i=0;i<10;i++)
    {
       for(j=0;j<32;j++)
       {
          strcpy(file[i].key[j].keyname,"key"); /* scan the value from user and input */
          file[i].key[j].count = 0;
       }
    }

This compile with gcc 4.4 --

int main() {
struct keyword
    {
        char keyname[5];
        int count;
    };
struct keyword files[4] = { {"void",0},{"int",4},{"long",8},{"utyp",12} };
  return 0;
}

Guess it depends on the compiler you are using.

Slightly modified @Gopi's code,

char keywords[32][]={"void","int" ......}; //Holds all the needed keywords,fill upto last desired keyword
for(j=0;j<32;j++)  //takes each file structure (10 file structure)
{
    for(i=0;i<10;i++)
    {
       //updates the 32 keynames and its count
       strcpy(file[i].key[j].keyname,keywords[j]); 
       file[i].key[j].count = 0;
    }
}

Using standard C, there isn't a way to avoid a loop such as:

for (int i = 0; i < 10; i++)
    file[i] = (struct files){ { { "auto", 0 }, { "break", 0 },
                  { "case", 0 }, { "char", 0 }, { "const", 0 },
                  { "continue", 0 }, { "default", 0 }, { "do", 0 },
                  { "double", 0 }, { "else", 0 }, { "enum", 0 },
                  { "extern", 0 }, { "float", 0 }, { "for", 0 },
                  { "goto", 0 }, { "if", 0 }, { "int", 0 },
                  { "long", 0 }, { "register", 0 }, { "return", 0 },
                  { "short", 0 }, { "signed", 0 }, { "sizeof", 0 },
                  { "static", 0 }, { "struct", 0 }, { "switch", 0 },
                  { "typedef", 0 }, { "union", 0 }, { "unsigned", 0 },
                  { "void", 0 }, { "volatile", 0 }, { "while", 0 },
                } };

This uses a C99 compound literal to initialize each row.

GCC has an extension that allows you to do it all in initializers (where you must put a space before the ellipsis to avoid problems with the 'maximal munch' rule):

struct files file[10] = { [0 ... 9] = { { { "auto", 0 }, { "break", 0 },
                  { "case", 0 }, { "char", 0 }, { "const", 0 },
                  { "continue", 0 }, { "default", 0 }, { "do", 0 },
                  { "double", 0 }, { "else", 0 }, { "enum", 0 },
                  { "extern", 0 }, { "float", 0 }, { "for", 0 },
                  { "goto", 0 }, { "if", 0 }, { "int", 0 },
                  { "long", 0 }, { "register", 0 }, { "return", 0 },
                  { "short", 0 }, { "signed", 0 }, { "sizeof", 0 },
                  { "static", 0 }, { "struct", 0 }, { "switch", 0 },
                  { "typedef", 0 }, { "union", 0 }, { "unsigned", 0 },
                  { "void", 0 }, { "volatile", 0 }, { "while", 0 },
              } } };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM