Mateusz
Mateusz

Reputation: 3

Variable-sized object may not be initialized (But with a twist) C

This code...

//variables
int tileSize = 20;
int playerSize = 5;
int map[tileSize][tileSize] =
{
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
};

...gives me this error

mateusz@mateusz-MS-7693:~/Desktop/3ds-Projects/Platform_game$ make
main.c
/home/mateusz/Desktop/3ds-Projects/Platform_game/source/main.c: In function 'main':
/home/mateusz/Desktop/3ds-Projects/Platform_game/source/main.c:21:5: error: variable-sized object may not be initialized
21 |     int map[tileSize][tileSize] =
   |     ^~~
make[1]: *** [/opt/devkitpro/devkitARM/base_rules:85: main.o] Error 1
make: *** [Makefile:155: all] Error 2

But when i put "20" instead of "tileSize" in the map variable size like so:

//variables
int tileSize = 20;
int playerSize = 5;
int map[20][20] =
{
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
};

the error dissapears and the code compiles just fine.

Here is the full code:

#include <citro2d.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#define SCREEN_WIDTH  400
#define SCREEN_HEIGHT 240

int main(int argc, char* argv[]){

gfxInitDefault();
C3D_Init(C3D_DEFAULT_CMDBUF_SIZE);
C2D_Init(C2D_DEFAULT_MAX_OBJECTS);
C2D_Prepare();
consoleInit(GFX_BOTTOM, NULL);
C3D_RenderTarget* top = C2D_CreateScreenTarget(GFX_TOP, GFX_LEFT);

//variables
int tileSize = 20;
int playerSize = 5;
int map[20][20] =
{
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
};
float Pos[2] = {100,100};

u32 PlayerColor = C2D_Color32(0x10,0x20,0xF0,0xFF);
u32 tileColor   = C2D_Color32(0x10,0xF0,0x90,0xFF);
u32 clrClear    = C2D_Color32(0x10,0x10,0x10,0xFF);

// Main loop
while (aptMainLoop())
{
    hidScanInput();
    u32 kDown = hidKeysDown();
    if (kDown & KEY_START) break;

    
    C3D_FrameBegin(C3D_FRAME_SYNCDRAW);
    C2D_TargetClear(top, clrClear);
    C2D_SceneBegin(top);
    
    C2D_DrawRectSolid(Pos[0], Pos[1],0,playerSize,playerSize, PlayerColor);
    for(int i = 0;i<SCREEN_WIDTH/tileSize;i++){
        for(int y = 0;y<SCREEN_HEIGHT/tileSize;y++){
            if(map[i][y]){
                C2D_DrawRectSolid(i*tileSize, y*tileSize,0,tileSize,tileSize, tileColor);
            }
        }
    }

    C3D_FrameEnd(0);
}

gfxExit();
C2D_Fini();
C3D_Fini();
return 0;
}

Why can't i put the "tileSize" variable inside the map size?

Upvotes: 0

Views: 74

Answers (2)

Aasai
Aasai

Reputation: 11

In your code //variables int tileSize = 20; is compile time constant. Before compilation it will substitute tileSize. That is why it is showing error.

Recommend you to use macro #define to define tileSize before main() and it will work.

#define tileSize 20

Upvotes: -1

Some programmer dude
Some programmer dude

Reputation: 409266

tileSize is not a compile-time constant value. It's a run-time variable, whose value isn't known at compile-time.

In C you need compile-time constants as array size if you want to be able to initialize the array at definition.

The most common way to do that is to use macros:

#define TILE_SIZE 20

int map[TILE_SIZE][TILE_SIZE] = { ... };

Another possibility is to use enumerations:

enum
{
    tileSize = 20
};

int map[tileSize][tileSize] = { ... };

Upvotes: 4

Related Questions