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

Noah Vawter nvawter at media.mit.edu
Wed Mar 30 18:38:17 CEST 2011


On Mar 30, 2011, at 11:19 AM, Tom Wiltshire wrote:

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

I would say the distinction is this:

If you're going to use only one of that structure, don't bother to  
typedef it.

If you're going to use a bunch of that structure and in various ways,  
then typedef it.


I will add this, though, part of the benefit of software is that you  
can cheaply use many instances of something, so usually you will end  
up making typedefs.  Nowadays, I always start with typedefs b/c more  
often than not I'm using them in ways that make structs alone harder  
to use.


>
>
> 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
>
> _______________________________________________
> 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