Q:

How to correctly use functions that return pointers to multiple typedef'ed types in Arduino IDE?

I have a program that typedef's two structs. Each one has a corresponding function that returns a pointer to it....
typedef struct {
  int x1;
} x;

x *a;

x *f(void) {
  return a;    
}

typedef struct {
    int  y1;
} y;

y *b;     

y *g(void) {
  return b;
}

void loop(void) {

}

void setup(void) {

}

This program produces the following error when compiled under the Arduino IDE...
sketch_nov10a:18: error: 'y' does not name a type

 y *g(void)

If we copy/paste to switch the order of the two blocks like so...
typedef struct {
    int  y1;
} y;

y *b;     

y *g(void) {
  return b;
}

typedef struct {
  int x1;
} x;

x *a;

x *f(void) {
  return a;    
}

void loop(void) {

}

void setup(void) {

}

...then we instead get the following error...
sketch_nov10a:18: error: 'x' does not name a type

x *f(void)

...so apparently the Arduino IDE only supports functions that return pointers to the first typedef'ed type defined in the file?
Is there a rational to this? How would you implement the above code to get it to compile under the Arduino IDE (it works fine in gcc). 

A:

The Arduino IDE, rather annoyingly, tries to "help" you by generating function prototypes for functions, something it sometimes fails to do.
You can make your code compile by adding one yourself:
typedef struct {
  int x1;
} x;

x *a;

x *f(void) {
  return a;    
}

typedef struct {
    int  y1;
} y;

y *b;     

y *g(void);    // <------------------ ADD THIS
y *g(void) {
  return b;
}

void loop(void) {

}

void setup(void) {

}

I have a post about how to avoid the quirks in the IDE which may help you.
If you just move your code into a separate .CPP file and leave the main sketch blank, it will compile, if you add this line to the start of the .CPP file:
#include <Arduino.h>

The Arduino IDE uses gcc, so it isn't a problem with the compiler exactly, it is a problem with how it "helps" you.

