0

I am new to C. I created a multidimensional array and I created hardcoded the size into the second square bracket(3). it compiled and I got my answer. However, i saw in a tutorial that someone created a const integer variable and put the variable name in the second square bracket and his code compiled. However when I tried mine(the exact same thing) it did not compile. See my code below

int main()
 16 {
 17 
 18 
 19         int uchegrade[] = {90, 87, 86};
 20 
 21 
 22 
 23 
 24 
 25
 26         int const columns = 3;
 27         int threegrades[][columns] = {
 28 {88, 78, 91},
 29  {81, 81, 67},
 30   {77, 78, 98}
 31 
 32         };
 33 
 34 
 35         printf("%d\n", uchegrade[3]);
 36         printf("physics score is %d marks\n", threegrades[1][2]);
 37 
 38         return 0;
 39 }
                                                         

below is the error i kept getting

multidimensionalarray.c: In function ‘main’:
multidimensionalarray.c:27:2: error: variable-sized object may not be initialized
   27 |  int threegrades[][columns] = {
      |  ^~~
multidimensionalarray.c:28:19: warning: excess elements in array initializer
   28 | /*first student*/{88, 78, 91},
      |                   ^~
multidimensionalarray.c:28:19: note: (near initialization for ‘threegrades[0]’)
multidimensionalarray.c:28:23: warning: excess elements in array initializer
   28 | /*first student*/{88, 78, 91},
      |                       ^~
multidimensionalarray.c:28:23: note: (near initialization for ‘threegrades[0]’)
multidimensionalarray.c:28:27: warning: excess elements in array initializer
   28 | /*first student*/{88, 78, 91},
      |                           ^~
multidimensionalarray.c:28:27: note: (near initialization for ‘threegrades[0]’)
multidimensionalarray.c:29:19: warning: excess elements in array initializer
   29 | /* 2nd student*/ {81, 81, 67},
      |                   ^~
multidimensionalarray.c:29:19: note: (near initialization for ‘threegrades[1]’)
multidimensionalarray.c:29:23: warning: excess elements in array initializer
   29 | /* 2nd student*/ {81, 81, 67},
      |                       ^~
multidimensionalarray.c:29:23: note: (near initialization for ‘threegrades[1]’)
multidimensionalarray.c:29:27: warning: excess elements in array initializer
   29 | /* 2nd student*/ {81, 81, 67},
      |                           ^~
multidimensionalarray.c:29:27: note: (near initialization for ‘threegrades[1]’)
multidimensionalarray.c:30:19: warning: excess elements in array initializer
   30 | /*3rd student*/  {77, 78, 98}
      |                   ^~
multidimensionalarray.c:30:19: note: (near initialization for ‘threegrades[2]’)
multidimensionalarray.c:30:23: warning: excess elements in array initializer
   30 | /*3rd student*/  {77, 78, 98}
      |                       ^~
multidimensionalarray.c:30:23: note: (near initialization for ‘threegrades[2]’)
multidimensionalarray.c:30:27: warning: excess elements in array initializer
   30 | /*3rd student*/  {77, 78, 98}
      |                           ^~
multidimensionalarray.c:30:27: note: (near initialization for ‘threegrades[2]’)
multidimensionalarray.c:27:6: error: array size missing in ‘threegrades’
   27 |  int threegrades[][columns] = {
      |      ^~~~~~~~~~~

klinks@klinks-VirtualBox:~/cprogramming$

Uche
  • 13
  • 5
  • 1
    In C (as required by the C standard), variables are not compile-time constants as needed for array dimensions, even if they are declared with `const`. C does allow variable-length arrays (as an optional feature), but does not support initializing them. If you have a fixed length for the array dimensions, you can define it using a macro (`#define columns 3`) or an enumeration `enum { columns = 3 };`. If you must have a variable length, you cannot initialize the array, but you can assign values to the elements individually with ordinary assignment statements. – Eric Postpischil Jul 31 '21 at 12:04
  • In the future, when posting code, do not post it with line numbers. Readers could be able to copy the code, paste it into a file, and compile the file without further editing. If you need to indicate lines to match with error messages or other purposes, add a comment pointing out the particular line. – Eric Postpischil Jul 31 '21 at 12:05
  • Thanks a lot. i will probably just keep hardcoding the size of the array rather than using a variable. is that what you mean by using a macro? and thanks for telling me about the line numbers. – Uche Jul 31 '21 at 12:09

0 Answers0