Re: Question about a struct declaration



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
.



Relevant Pages

  • Re: Question about a struct declaration
    ... type thoughout the code is 'struct dummy' unless there is a later typedef. ... Decide how you want the programmer to refer to a struct. ... If you want opaque datatypes referred to though pointers of the type 'dummy *', ...
    (comp.lang.c)
  • Re: OT: Open Source, In The News
    ... it's not the typedef itself that is a problem. ... And only for pointers; if the struct is used in its entirety (passed by ... instantiation, so it reduces the temptation to use typedefs, and I don't ...
    (comp.unix.solaris)
  • Converting containers of smart pointers
    ... struct B: public A; ... typedef std::setAS; ... void F { ... //...fill as with some pointers... ...
    (comp.lang.cpp)
  • Re: Simple question, err... I think
    ... Your nodes contain no other indication of which pointers are valid, ... struct CountedObject ... int is_red; ... bool lament(char const s) ...
    (comp.programming)
  • Re: porting problems encountered
    ... Tru64 compiles long variables to size 8 bytes while VMS and HP-UX ... platform, the size of the structure would be 12 bytes. ... when it got to the AS/400 with 128 bit pointers. ... Using the struct from before: ...
    (comp.os.vms)

Loading