0

I have declared a 2D global array variable like so:

int grid_2d_array[ROWS][COLUMNS];

then in main I've to initialize it with hard-coded values:

grid_2d_array[ROWS][COLUMNS] = {{5, 7, 2, 8, 3, 6, 1, 4},
                                ....
                                {1, 6, 3, 2, 4, 8, 9, 5}
                                };

Example:

#include <stdio.h>

#define ROWS    9
#define COLUMNS 9

/* Global variable. */
int grid_2d_array[ROWS][COLUMNS];

int main() 
{
   /* Initialze the 2D array. */
   grid_2d_array[ROWS][COLUMNS] = {{5, 7, 2, 8, 3, 6, 1, 4},
                                   ....
                                   {1, 6, 3, 2, 4, 8, 9, 5}
                                  };

   return 0;
}

But when I try compiling the source code, GCC gives the following error:

source_file.c: In function ‘main’:
source_file.c:45:34: error: expected expression before ‘{’ token
 grid_2d_array[ROWS][COLUMNS] = {{5, 7, 2, 8, 3, 6, 1, 4},
                                ^

I'm not sure why GCC is not recognizing grid_2d_array as a global variable.

The problem goes away if I redeclare the aforementioned variable in main.

I'm running GCC version: gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04)

7
  • Please provide a proper minimal example of this that others can try to compile. Now it's unclear where the definition is and if something else affects the problem. Commented Oct 6, 2015 at 8:21
  • You need to allocate memory for your array and its nested arrays. Commented Oct 6, 2015 at 8:21
  • 1
    grid_2d_array[ROWS][COLUMNS] = specifies an element (which does not exist) of the array. Try removing the [ROWS][COLUMNS] and just use grid_2d_array = { /*your stuff goes here*/};. Commented Oct 6, 2015 at 8:24
  • Is there any reason why you do not use a std::array<std::array<int>> ? Commented Oct 6, 2015 at 8:24
  • @tobi303 Good question. Is this C or C++ (it can't be both)? Commented Oct 6, 2015 at 8:25

2 Answers 2

5

C and C++ arrays can be initialized only as a part of the definition statement:

int grid_2d_array[ROWS][COLUMNS] = {{5, 7, 2, 8, 3, 6, 1, 4},
                                    ....
                                    {1, 6, 3, 2, 4, 8, 9, 5}
                                   };

Assignment of multiple values into an array is not supported.

Sign up to request clarification or add additional context in comments.

Comments

2

In addition to FireAphis's answer, if you are under C99 you can initialize a pointer to array of ints (not a 2D array) outside his definition using compound literals:

int (*grid_2d_array)[COLUMNS]; /* Pointer to array of n int's */

in main:

grid_2d_array = (int [ROWS][COLUMNS]){
    {5, 7, 2, 8, 3, 6, 1, 4},
    {1, 6, 3, 2, 4, 8, 9, 5}
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.