title: C - Creating and using an struct array? error: expected identifier or '(' before '[' token
body: <p>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:</p>
  
  <pre><code>int numWords = 10;
  
  typedef struct
  {
      char* word;
      int count;
  } wordsCount;
  
  wordsCount words[numWords];
  
  words[0].word = "Hello";
  words[0].count = 1;
  </code></pre>
  
  <p>First of all, when using the gcc compiler in Unix, I receive an warning, when compiling with the -Wall -pedantic -ansi flags, which reads <code>warning: ISO C90 forbids variable length array 'words' [-Wvla]</code>. And for the last two lines of my code snippet, I receive the error: <code>error: expected identifier or '(' before '[' token</code>.</p>
  
  <p>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. </p>
  
  <hr>
  
  <p><strong>EDIT:</strong>
  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.</p>
  
  <p>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: <code>"Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo" is a grammatically correct sentence.</code>, the output of the program shall be:</p>
  
  <p><code>Buffalo - 3
  a - 1
  buffalo - 5
  correct - 1
  sentence - 1</code>.</p>
  
  <p>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:</p>
  
  <pre><code>typedef struct
  {
      char* word;
      int count;
  } wordsCount;
  
  wordsCount** words = malloc(sizeof(*words) * numWords);
  
  words[0]-&gt;word = "Hello";
  words[0]-&gt;count = 2;
  
  printf("%s - %d\n", words[0]-&gt;word, words[0]-&gt;count);
  </code></pre>
  
  <p>*Note that <code>numWords</code> is an integer that changes according to how large the text file is.</p>
  
  <p>It compiles fine; however, I receive a seg fault at the first assignment statement <code>words[0]-&gt;word = "Hello";</code>.</p>
  
  <p>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.</p>
  
  <p><strong>EDIT 2</strong>
  I figured out the problem - I was pointer-happy and used way too many pointers to do what I want. I changed <code>wordsCount** words = malloc(sizeof(*words) * numWords);</code> to <code>wordsCount* words = malloc(sizeof(*words) * numWords);</code>. And then I realized I was doing necessary dereferencing when I did <code>words[0]-&gt;word = "Hello";</code>, so I changed it to <code>words[0].word = "Hello";</code>, and it works perfectly. Thanks for the help, guys. I'm not sure how to close a thread, or if I should post an answer to my own question? But this thread is now closed.</p>