Re: Question about a struct declaration
- From: Andrey Tarasevich <andreytarasevich@xxxxxxxxxxx>
- Date: Wed, 16 Apr 2008 11:06:07 -0700
Richard Heathfield wrote:
Andrey Tarasevich said:However, if one day you'll need to do something like that
typedef struct dummy *dummy;
Please don't hide pointers in typedefs.
Er... There are situations when you don't, and there are situations when that the correct thing to do.
If want to use the type as a pointer (i.e. keep its pointer nature exposed to the client), then hiding the fact that it is a pointer in a typedef is indeed a pretty useless idea.
However, if you want to declare a generic "handle" type, whose specific nature is not supposed to be exploited by the user in any way, a pointer hidden in a typedef is a standard, widely used and accepted idiom. A classic example would be the interface of 'pthreads' library, where 'pthread_t', 'pthread_mutex_t' etc. might easily be pointers (or might not be pointers). If they are in fact pointers in a given implementation, you'll probably see typedefs in the interface header file that'll look pretty much as the one above. And there's nothing wrong with it.
The method 2 might be OK, but, as others already mentioned, it won't
work in case when you need to self-refer to the struct type from inside
if its definition.
I'm surprised that nobody has yet mentioned the very simple solution to this problem: i.e. forward declaration.
typedef struct dummy_ dummy;
struct dummy_
{
dummy *prev;
dummy *next;
int data;
};
I don't exactly see how it applies to method 2 (since that's what I was talking about), since method 2 is specifically about the _anonymous_ struct.
As for the other methods, nobody mentioned that "simple solution to this problem" simply because nobody thinks there's a problem here. Your solution is in no way simpler that using struct tag to self-refer to a tagged struct type. All these methods are in the FAQ anyway.
--
Best regards,
Andrey Tarasevich
.
- Follow-Ups:
- Re: Question about a struct declaration
- From: Richard Heathfield
- Re: Question about a struct declaration
- References:
- Question about a struct declaration
- From: heavyz
- Re: Question about a struct declaration
- From: Andrey Tarasevich
- Re: Question about a struct declaration
- From: Richard Heathfield
- Question about a struct declaration
- Prev by Date: Re: How to eliminate this global variable, silent?
- Next by Date: Re: why this does n't works
- Previous by thread: Re: Question about a struct declaration
- Next by thread: Re: Question about a struct declaration
- Index(es):
Relevant Pages
|
Loading