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.
Thank you all for the suggestions. I decided to edit my post instead of responding to each individual comment to provide some clarification of my question. So, I now understand that you can't use variable length arrays in C. I can't officially accept any of the provided answers because my problem still exists, so a clearer explanation of what I am trying to do is warranted.
I am creating a program which counts the occurrences of each word in a text file and prints it out in ASCII order. For example, if the text file contains the following text: "Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo" is a grammatically correct sentence.
, the output of the program shall be:
Buffalo - 3
a - 1
buffalo - 5
correct - 1
sentence - 1
The words themselves are already stored into an array. So I want to create an array of structs that contain both the word and the count. I've revised my program to account for dynamically allocated memory. My updated code is:
typedef struct
{
char* word;
int count;
} wordsCount;
wordsCount** words = malloc(sizeof(*words) * numWords);
words[0]->word = "Hello";
words[0]->count = 2;
printf("%s - %d\n", words[0]->word, words[0]->count);
Note: numWords
is an integer that changes according to how large the text file is.
It compiles fine; however, I receive a seg fault at the first assignment statement words[0]->word = "Hello";
.
Am I wrong to assume that I can create a pointer to a pointer to structs? I thought the ->
operator does the deference of the struct pointer, and then point to the member "word." I do not understand why I am seg faulting. Any help would be appreciated. If making this edit warrants a new thread, please let me know so I can move it. Thanks.
I figured out the problem - I was pointer-happy and used way too many pointers to do what I wanted. I changed
wordsCount** words = malloc(sizeof(*words) * numWords);
to
wordsCount* words = malloc(sizeof(*words) * numWords);
And then I realized I was doing necessary dereferencing when I did
words[0]->word = "Hello";
so I changed it to
words[0].word = "Hello";
and it works perfectly.
This was the resulting code:
typedef struct
{
char* word;
int count;
} wordsCount;
wordsCount* words = malloc(sizeof(*words) * numWords);
words[0].word = "Hello";
words[0].count = 2;
printf("%s - %d\n", words[0]->word, words[0]->count);