csdccgbdk

SDCC / GBDK, Passing array of structs to function


I am attempting to pass a pair of arrays of structures to a function in C.

I must use the SDCC compiler as I am compiling for Gameboy using GBDK.

When I pass the array of structures I get an error as shown below, however, if I take the function definition out completely and just cut and paste the entire function body in place of the function call, then the code compiles and runs smoothly.

For brevity I am only including the code that is relevant.

// Struct definitions
struct point {
  UINT16 x, y;
  INT8 h;
  INT8 h_tot;
};

typedef struct point Point;

Point layer1[25];
Point layer2[49];

// Function declarations
void createMap2();
void updateLayers( Point layer2[], Point layer1[], UINT16 limLo, UINT16 limHi, UINT16 limLast, INT8 layer );

void createMap2(){
  UINT16 limLo   = 0;
  UINT16 limHi   = 7;
  UINT16 limLast = 5;
  INT8 layer = 2;

  updateLayers( layer2, layer1, limLo, limHi, limLast, layer );

  // do more stuff
}

void updateLayers( Point layer2[], Point layer1[], UINT16 limLo, UINT16 limHi, UINT16 limLast, INT8 layer ){
  // Do things within function
}

When I try to compile using GBDK and the SDCC compiler I get the following error:

C:\Users\terri\gbdk\testgame>..\bin\lcc -V -Wa-l -Wl-m -Wl-j -DUSE_SFR_FOR_REG -c -o testgame.o testgame.c
testgame.c(457):error *** Actual Argument type different from declaration 1

When I remove the call to the function, and simply cut and paste the full function body in place of the function call, then it compiles smoothly, and I get a nice little randomized gameboy tile map.

An image can be seen here as I don't have the rep for images:

TestgameScreen


Solution

  • You probably need to pass Point* to the function rather than Point[] and you will probably need to pass the length of the arrays or terminate them with some sentinel value. Older compilers would not accept that notation with the brackets. I didn't even realize that they would accept that now.

    Things have been added to C over the years, but compilers often do not support them for years and years. I recall compilers complaining about int[5] and int[10] being different types, particularly in regard to function arguments. I pretty much always use pointer types and lengths when passing an array to a variable. Anyway, I'm pretty sure that is your problem.