[sdiy] OT (but very synthy): C question

Olivier Gillet ol.gillet at gmail.com
Tue Mar 29 21:10:02 CEST 2011


> No, that's a typedef type definition.  What you mean is an incomplete
> forward definition of a struct:
>
> struct LFO;

I think the difference stems from the fact that in C++, every time you
declare a struct MyStruct, the compiler does, in the background
something that would be very similar to a "typedef struct MyStruct
MyStruct;" ("copying" a tag declaration into the types declarations).

This:

struct MyStruct {
  int x;
};
MyStruct my_var;

is legit C++, but in C, you have to write either:

struct MyStruct {
  int x;
};
typedef struct MyStruct MyStruct;  // Declare a type from the tag
MyStruct my_var;

Or:

struct MyStruct {
  int x;
};
struct MyStruct my_var;  // Use the tag, not the type.

In C++ you have the tags turned into types automatically, not in C ;
hence the forward declaration of a type is simply "struct MyStruct;"
in C++ ; but you need a typedef in C - otherwise you would be forward
declaring only a tag!

To get back to Tom's example, it could also be solved with:

// Forward declaration of a tag
struct LFO;
struct MIDI;

// Declarations using the forward-declared tags.
void update_midi(struct MIDI* midi, struct LFO* lfo);
void update_lfo(struct MIDI* midi, struct LFO* lfo);

Am I getting the tag/type difference correct?

Olivier



More information about the Synth-diy mailing list