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

Tom Wiltshire tom at electricdruid.net
Wed Mar 30 13:05:56 CEST 2011


Thanks to everyone who helped me with this. I've sorted it out much along the lines that Olivier shows below.

It's annoying that in C you can forward-declare a struct, but can't do the same thing with a typedef.
The following is possible..

typedef struct LFO LFO;

..but this just forward-declares the struct, then creates a type from it. Since it's a definition (of a new type) you can't use this like you would a forward declaration. If you attempt to, you'll get a "redefinition of typedef" error.
This means that you finish up having to scatter the 'struct' keyword all over your function prototypes (like Olivier does below) because you can't typedef the struct again, and makes the 'typedef' keyword considerably less useful than it could have been in my view.

Perhaps further thinking about where I put things and the various scopes will let me find a way around this, but for now, I've spent enough time on it and need to get back to the actual job at hand rather than fighting with the language.

Thanks again to everyone who helped me work through this problem. I understand much more now than I did 48hrs ago.

Regards,
Tom

PS: I promise not to use 'this', even though it's only a keyword in a language that I'm not actually using (C++)! ;)


On 29 Mar 2011, at 20:10, Olivier Gillet wrote:
> 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
> _______________________________________________
> Synth-diy mailing list
> Synth-diy at dropmix.xs4all.nl
> http://dropmix.xs4all.nl/mailman/listinfo/synth-diy




More information about the Synth-diy mailing list