user7859: 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). 

user7859: Cause
Ok, I've pinned down the root of this issue.
The arduino-builder program is responsible for mangling the sketch INO file into a cpp file to be fed into gcc. Among other things,  it scans the file for function definitions and then adds prototypes for all the found functions.
The prototypes are added as a block just before the first function declaration in the file. 
Unfortunately this strategy fails any time there is the following sequence....

a function definition
a type definition
a function definition that depends on the type defined in #2

...since the prototype for the function defined in #3 ends up being added before the type definition in #2 on which it depends, creating the compilation error.
The code responsible for this behavior is here...
https://github.com/arduino/arduino-builder/blob/b91cea8c8a42803b0b40dcdde2d1165c4e225457/src/arduino.cc/builder/ctags_runner.go#L83
A more robust strategy would be to add each function prototype before the first function definition that calls the function being prototyped. 
Workaround
In the meantime, a workaround is to move all type definitions to be before the first function definition in the source code. 
Rearranging the above example program like so...
typedef struct {
  int x1;
} x;

typedef struct {
    int  y1;
} y;



x *a;

x *f(void) {
  return a;    
}


y *b;     

y *g(void) {
  return b;
}


void loop(void) {

}

void setup(void) {

}

...resolves the error, at the expense of code clarity. 

user10794: 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.

7859: <p>I have a program that typedef's two structs. Each one has a corresponding function that returns a pointer to it....</p>

<pre><code>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) {

}
</code></pre>

<p>This program produces the following error when compiled under the Arduino IDE...</p>

<pre><code>sketch_nov10a:18: error: 'y' does not name a type

 y *g(void)
</code></pre>

<p>If we copy/paste to switch the order of the two blocks like so...</p>

<pre><code>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) {

}
</code></pre>

<p>...then we instead get the following error...</p>

<pre><code>sketch_nov10a:18: error: 'x' does not name a type

x *f(void)
</code></pre>

<p>...so apparently the Arduino IDE only supports functions that return pointers to the first typedef'ed type defined in the file?</p>

<p>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 <code>gcc</code>). </p>
 7859: <h1>Cause</h1>

<p>Ok, I've pinned down the root of this issue.</p>

<p>The <code>arduino-builder</code> program is responsible for mangling the sketch INO file into a cpp file to be fed into <code>gcc</code>. Among other things,  it scans the file for function definitions and then adds prototypes for all the found functions.</p>

<p>The prototypes are added as a block just before the first function declaration in the file. </p>

<p>Unfortunately this strategy fails any time there is the following sequence....</p>

<ol>
<li>a function definition</li>
<li>a type definition</li>
<li>a function definition that depends on the type defined in #2</li>
</ol>

<p>...since the prototype for the function defined in #3 ends up being added before the type definition in #2 on which it depends, creating the compilation error.</p>

<p>The code responsible for this behavior is here...</p>

<p><a href="https://github.com/arduino/arduino-builder/blob/b91cea8c8a42803b0b40dcdde2d1165c4e225457/src/arduino.cc/builder/ctags_runner.go#L83" rel="nofollow noreferrer">https://github.com/arduino/arduino-builder/blob/b91cea8c8a42803b0b40dcdde2d1165c4e225457/src/arduino.cc/builder/ctags_runner.go#L83</a></p>

<p>A more robust strategy would be to add each function prototype before the first function definition that calls the function being prototyped. </p>

<h1>Workaround</h1>

<p>In the meantime, a workaround is to move all type definitions to be before the first function definition in the source code. </p>

<p>Rearranging the above example program like so...</p>

<pre><code>typedef struct {
  int x1;
} x;

typedef struct {
    int  y1;
} y;



x *a;

x *f(void) {
  return a;    
}


y *b;     

y *g(void) {
  return b;
}


void loop(void) {

}

void setup(void) {

}
</code></pre>

<p>...resolves the error, at the expense of code clarity. </p>
 10794: <p>The Arduino IDE, rather annoyingly, tries to "help" you by generating function prototypes for functions, something it sometimes fails to do.</p>

<p>You can make your code compile by adding one yourself:</p>

<pre><code>typedef struct {
  int x1;
} x;

x *a;

x *f(void) {
  return a;    
}

typedef struct {
    int  y1;
} y;


y *b;     

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


void loop(void) {

}

void setup(void) {

}
</code></pre>

<p>I have a <a href="http://www.gammon.com.au/forum/?id=12625" rel="noreferrer">post about how to avoid the quirks in the IDE</a> which may help you.</p>

<p>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:</p>

<pre><code>#include &lt;Arduino.h&gt;
</code></pre>

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