Global struct array invalid outside main
Posted: Sat Jun 22, 2024 1:14 pm
Hello fellow devkiters,
I'm new to the GBA development scene and have run across a problem when trying to access an array of structs across multiple .c files.
Given the following structure sprite.h/sprite.c defines my Sprite struct, buffer.h/buffer.c defines a fixed size array of Sprites.
This array is valid in main.c but invalid in sprite.c (or any other .c file/object).
Further testing of an array of shorts works as expected and is valid across any .c file.
Any help to fill in the gap of knowledge I'm missing would be most appreciated
Here is my code below, my Makefile is boilerplate from example\gba\template
sprite.h
sprite.c
buffer.h
buffer.c
I'm new to the GBA development scene and have run across a problem when trying to access an array of structs across multiple .c files.
Given the following structure sprite.h/sprite.c defines my Sprite struct, buffer.h/buffer.c defines a fixed size array of Sprites.
This array is valid in main.c but invalid in sprite.c (or any other .c file/object).
Further testing of an array of shorts works as expected and is valid across any .c file.
Any help to fill in the gap of knowledge I'm missing would be most appreciated
Here is my code below, my Makefile is boilerplate from example\gba\template
sprite.h
Code: Select all
#ifndef _SPRITE_
#define _SPRITE_
#include "global.h"
typedef struct Sprite
{
u16 sliceCount;
const Rect* pSlices;
const u16 *map;
} Sprite;
void sprite_make(const u16 *map, const Rect* pSlices, const u16 sliceCount, Sprite* pOutSprite);
#endif
Code: Select all
#include "sprite.h"
#include "buffer.h"
void sprite_make(const u16 *map, const Rect* pSlices, const u16 sliceCount, Sprite* pOutSprite)
{
// Invlaid pOutSprite but valid g_spriteCount and g_tmp
pOutSprite = &g_spriteBuffer[g_spriteCount++];
pOutSprite->map = map;
pOutSprite->pSlices = pSlices;
pOutSprite->sliceCount = sliceCount;
}
Code: Select all
#ifndef _GAMEBUFFERS_
#define _GAMEBUFFERS_
#include "global.h"
#include "sprite.h"
#define SPRITE_BUFFER_SIZE 10
// Valid in main.c invalid in sprite.c
extern Sprite g_spriteBuffer[];
extern u16 g_spriteCount;
// Works as expected valid from both sprite.c and main.c
extern u16 g_tmp[];
#endif
Code: Select all
#include "buffer.h"
Sprite g_spriteBuffer[SPRITE_BUFFER_SIZE];
u16 g_spriteCount = 0;
u16 g_tmp[] = {1, 3, 5 , 6};