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

Tom Wiltshire tom at electricdruid.net
Wed Mar 30 17:19:50 CEST 2011


Ok, typedef allows you to use an existing structure *as if* it were a new type, by creating a synonym for that structure - but that's by the by. The criticism was that the inability to forward-declare anything using the synonym makes the otherwise-useful feature a lot less useful, whether you understand it or not.

Can you give us "typedef sceptics" some example in which it really proves absolutely unmissable (and not just "makes the code a bit cleaner")? (I'm trying to learn C here, after all.)

T.


On 30 Mar 2011, at 15:06, Neil Johnson wrote:

> Typedef creates a synonym for an existing type, it does not in and of
> itself create a new type. Like any feature in C (or any other
> language) it has its uses, but it can also be misused, either by those
> who *really* know what they're doing, or those who simply *don't*.
> 
> Not understanding a concept does not make the concept dumb, it is more
> a comment about the commenter...
> 
> Neil
> 
> 
> On 30/03/2011, Jay Vaughan <jayv at synth.net> wrote:
>> I agree, typedef is really useless.
>> 
>> ;
>> --
>> seclorum
>> 
>> On Mar 30, 2011, at 14:02, Thomas Strathmann <thomas at pdp7.org> wrote:
>> 
>>> On 3/30/11 13:05 , Tom Wiltshire wrote:
>>>> 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.
>>> 
>>> For whatever it's worth, I'm actually in favour of using the struct
>>> keyword instead of using typedef. It might not be that pretty, but at
>>> least you know exactly from the type-signature of a function prototype
>>> what kind of values it deals with without having to look up the
>>> user-defined types first. I consider it a little more readable, but YMMV.
>>> 
>>>   Thomas
>>> _______________________________________________
>>> Synth-diy mailing list
>>> Synth-diy at dropmix.xs4all.nl
>>> http://dropmix.xs4all.nl/mailman/listinfo/synth-diy
>> _______________________________________________
>> Synth-diy mailing list
>> Synth-diy at dropmix.xs4all.nl
>> http://dropmix.xs4all.nl/mailman/listinfo/synth-diy
>> 
> 
> 
> -- 
> http://www.njohnson.co.uk
> _______________________________________________
> 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