mercredi 16 avril 2014

C - création et utilisation d'un tableau de struct ? erreur : identificateur attendu ou ' (' avant ' [' jeton - Stack Overflow


So I am trying to build and use a struct array in C, and I am encountering a few errors of which I cannot resolve. The code that I have is:


int numWords = 10;

typedef struct
{
char* word;
int count;
} wordsCount;

wordsCount words[numWords];

words[0].word = "Hello";
words[0].count = 1;

First of all, when using the gcc compiler in Unix, I receive an warning, when compiling with the -Wall -pedantic -ansi flags, which reads warning: ISO C90 forbids variable length array 'words' [-Wvla]. And for the last two lines of my code snippet, I receive the error: error: expected identifier or '(' before '[' token.


Could anybody tell me why I am getting these errors? I've Googled many different typedef and struct examples, and mine mirrors that of those examples.




#define NUM_WORDS 10

typedef struct
{
char* word;
int count;
} wordsCount;

wordsCount words[NUM_WORDS];

This will sufficiently allocate the memory you will need in order to start assigning values.




The compiler needs to know how big the 'words' array should be, so when it comes across your array initialization, it expects to find a size between the brackets. But it finds the 'numWords' variable instead and it can't use that. The preprocessor will not replace 'numWords' with '10'. It will do so when you change this


int numWords = 10;

to this


#define numWords 10

That should work. The preprocessor will then replace every instance of the word 'numWords' in your code with '10', so that the array initialization will read:


wordsCount words[10];



There are two main problems.


The first, ably identified by the other answers, is that you can't use a VLA at global scope. Since numWords is an int variable, it is not a constant and array dimensions at file scope must be constants.


I'd recommend:


enum { numWords = 10 };

See also static const vs #define in C.


The second problem is that you are writing assignments at global scope, and that is not allowed either. You can provide an initializer, though:


enum { numWords = 10 };

typedef struct
{
char* word;
int count;
} wordsCount;

static wordsCount words[numWords] =
{
{ "Hello", 1 },
};

You wouldn't want to let code outside this file access your variables, would you?




This syntax not accepted in C, accepted in C++ but requires const though. You would use macro instead:


#define numWords 10

As for array initialization you would do it as:


typedef struct
{
const char* word;
int count;
} wordsCount;

wordsCount words[numWords] = {
{"AAAA", 1},
{"BBBB", 2},
{"CCCC", 3},
{NULL , 0},
};

if you will never use the type wordsCount agine, you can do it as:


struct
{
const char* word;
int count;
} words[numWords] = {
{"AAAA", 1},
{"BBBB", 2},
{"CCCC", 3},
{NULL , 0},
};


So I am trying to build and use a struct array in C, and I am encountering a few errors of which I cannot resolve. The code that I have is:


int numWords = 10;

typedef struct
{
char* word;
int count;
} wordsCount;

wordsCount words[numWords];

words[0].word = "Hello";
words[0].count = 1;

First of all, when using the gcc compiler in Unix, I receive an warning, when compiling with the -Wall -pedantic -ansi flags, which reads warning: ISO C90 forbids variable length array 'words' [-Wvla]. And for the last two lines of my code snippet, I receive the error: error: expected identifier or '(' before '[' token.


Could anybody tell me why I am getting these errors? I've Googled many different typedef and struct examples, and mine mirrors that of those examples.



#define NUM_WORDS 10

typedef struct
{
char* word;
int count;
} wordsCount;

wordsCount words[NUM_WORDS];

This will sufficiently allocate the memory you will need in order to start assigning values.



The compiler needs to know how big the 'words' array should be, so when it comes across your array initialization, it expects to find a size between the brackets. But it finds the 'numWords' variable instead and it can't use that. The preprocessor will not replace 'numWords' with '10'. It will do so when you change this


int numWords = 10;

to this


#define numWords 10

That should work. The preprocessor will then replace every instance of the word 'numWords' in your code with '10', so that the array initialization will read:


wordsCount words[10];


There are two main problems.


The first, ably identified by the other answers, is that you can't use a VLA at global scope. Since numWords is an int variable, it is not a constant and array dimensions at file scope must be constants.


I'd recommend:


enum { numWords = 10 };

See also static const vs #define in C.


The second problem is that you are writing assignments at global scope, and that is not allowed either. You can provide an initializer, though:


enum { numWords = 10 };

typedef struct
{
char* word;
int count;
} wordsCount;

static wordsCount words[numWords] =
{
{ "Hello", 1 },
};

You wouldn't want to let code outside this file access your variables, would you?



This syntax not accepted in C, accepted in C++ but requires const though. You would use macro instead:


#define numWords 10

As for array initialization you would do it as:


typedef struct
{
const char* word;
int count;
} wordsCount;

wordsCount words[numWords] = {
{"AAAA", 1},
{"BBBB", 2},
{"CCCC", 3},
{NULL , 0},
};

if you will never use the type wordsCount agine, you can do it as:


struct
{
const char* word;
int count;
} words[numWords] = {
{"AAAA", 1},
{"BBBB", 2},
{"CCCC", 3},
{NULL , 0},
};

0 commentaires:

Enregistrer un commentaire