0

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

G. Putnam
  • 1,262
  • 5
  • 10
  • 1
    What if you replace VLA `Point layer2[]` with plain pointers `Point *layer2`? Except for that, I think the error is in the code you didn't show. – KamilCuk Jan 29 '19 at 00:11
  • What _exactly_ is there at the line 457 of _your_ `testgame.c` file? – user58697 Jan 29 '19 at 02:18

1 Answers1

0

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.

Bob Shaffer
  • 635
  • 3
  • 13
  • Thanks to Kamil for the quick suggestion and Bob for the more detailed answer. Changing the function call to: `void updateLayers( Point* layerA, Point* layerB, UINT16 limLo, UINT16 limHi, UINT16 limLast, INT8 layer )` did allow the code to compile. Still has some other issues, yet they're not related to this question. Strange, as every other answer said that `struct myStruct stArray[]` was valid for C. It is the Small Device C Compiler though, so it is probably a smaller subset of C. – G. Putnam Jan 29 '19 at 19:56
  • Valid for the current revision of the C standard, and I believe was added in the C99 revision of the standard. Since the device you are programming predates that by at least 10 years, either the compiler or SDK may need you to use the older ANSI C standard to get things compiled. It will probably save you some headaches if you can determine exactly which C standard you need to follow and look at the differences referenced and described here: https://en.wikipedia.org/wiki/ANSI_C#C89 – Bob Shaffer Jan 29 '19 at 22:55
  • SDCC is known not to support a lot of C features. You need to take a look at the GBDK/SDCC documentation. You cannot even pass structs as function arguments. – Tara Jun 13 '21 at 02:13