2

Consider the following 3D matrix

char ShapesArray[2] = { 
  (char[4][4]){
    { 0, 1, 0, 0 }, //I
    { 0, 1, 0, 0 },
    { 0, 1, 0, 0 },
    { 0, 1, 0, 0 }
  }, 
  (char[3][3]){
    { 0, 1, 0 },    //J
    { 0, 1, 0 },
    { 1, 1, 0 }
  }
};

by using

int i = sizeof(ShapesArray[0]));

I would expect 16 as result.
But the result in this case is: 1

What am I missing here?

Watercayman
  • 7,970
  • 10
  • 31
  • 49
OrElse
  • 9,709
  • 39
  • 140
  • 253
  • 1
    `char ShapesArray[2]` is defining a 1D array of two `char` elements. – Eugene Sh. Jul 26 '19 at 19:20
  • @EugeneSh. so this is not actually a 3D array? – OrElse Jul 26 '19 at 19:22
  • 3
    Your compiler should throw some warnings about excess initializers – Eugene Sh. Jul 26 '19 at 19:22
  • I wish it would, but it has no warnings at all. – OrElse Jul 26 '19 at 19:24
  • 1
    MSVC generates **two** warnings of *'initializing': 'char' differs in levels of indirection from ...* and **one** error *COFF format cannot statically initialize '_ShapesArray' with 1 byte(s) of an address.* But the array `char ShapesArray[2]` is supposed to have two `char` elements so `sizeof(ShapesArray[0])` (one closing parenthesis) is `1` so that part is correct. – Weather Vane Jul 26 '19 at 19:27
  • 1
    I think the static `sizeof` cannot be used here. I would define a `struct` with `size_t size_x, size_y;` – Neil Jul 26 '19 at 19:32
  • @WeatherVane Should i change my compiler? If i am not mistaken, arduino uses GCC behind the scenes – OrElse Jul 26 '19 at 19:32
  • 2
    Try compiler option `-Wall` – Weather Vane Jul 26 '19 at 19:33
  • 1
    First, lookup if there's a way to turn errors up? eg, `-Wall -Wextra`. – Neil Jul 26 '19 at 19:34
  • @NeilEdelman I think i got your point. By using something like ShapesArraySize { int *Array; size_t size; }; – OrElse Jul 26 '19 at 19:37
  • 1
    To enable warnings add `-Wall -Wextra -pedantic` to your `gcc/clang` compile string (also consider adding `-Wshadow` to warn on shadowed variables). For **VS** (`cl.exe` on windows), use `/W3`. – David C. Rankin Jul 26 '19 at 19:41
  • MSVC "/Wall Displays all warnings displayed by /W4 and all other warnings that /W4 does not include," if you want to really turn it up a notch, but https://stackoverflow.com/questions/16883037/remove-secure-warnings-crt-secure-no-warnings-from-projects-by-default-in-vis. – Neil Jul 26 '19 at 21:21

1 Answers1

3

char ShapesArray[2] is an array of two chars. Hence the first element's size is 1. Turn on the compiler warnings:

<source>: In function 'main':

<source>:4:2: error: initialization of 'char' from 'char (*)[4]' makes integer from pointer without a cast [-Werror=int-conversion]

    4 |  (char[4][4]) {

      |  ^

<source>:4:2: note: (near initialization for 'ShapesArray[0]')

<source>:10:2: error: initialization of 'char' from 'char (*)[3]' makes integer from pointer without a cast [-Werror=int-conversion]

   10 |  (char[3][3]) {

      |  ^

<source>:10:2: note: (near initialization for 'ShapesArray[1]')

cc1: all warnings being treated as errors

Compiler returned: 1

What the compiler is saying is that you're initializing chars with char (*)[4] and char (*)[3] which is wrong. It wouldn't even compile with a C++ compiler.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
  • Actually it does with arduino. Sketch uses 1972 bytes (6%) of program storage space. Maximum is 30720 bytes.... Will try that though. Thank you all – OrElse Jul 26 '19 at 19:46