36

Suppose I have a number of C structs for which I would like a particular set of functions to operate upon.

I'm wondering if the following is a legitimate approach:

typedef struct Base {
     int exampleMember;
     // ...
} Base;

typedef struct Foo {
     Base base;
     // ...
} Foo;

typedef struct Bar {
     Base base;
     // ...
} Bar;

void MethodOperatesOnBase(void *);

void MethodOperatesOnBase(void * obj)
{
     Base * base = obj;
     base->exampleMember++;
}

In the example you'll notice that both structs Foo and Bar begin with a Base member.

And, that in MethodOperatesOnBase, I cast the void * parameter to Base *.

I'd like to pass pointers to Bar and pointers to Foo to this method and rely on the first member of the struct to be a Base struct.

Is this acceptable, or are there some (possibly compiler-specific) issues I need to be aware of? (Such as some sort of packing/padding scheme that would change the location of the first member of a struct?)

11
  • 2
    Any reason why you don't just use C++? Sep 5, 2011 at 20:43
  • Yes. But point well taken. I am trying to imitate a super-simple inheritance mechanism for a limited number of structs in my project.
    – Steve
    Sep 5, 2011 at 20:46
  • 2
    I understand what you're saying, but I can't for this project.
    – Steve
    Sep 5, 2011 at 20:51
  • 1
    Is this homework? Been out of school for 10 years, but thanks. LOL
    – Steve
    Sep 5, 2011 at 20:53
  • 4
    @Jonathan Grynspan: It's a legitimate question. Suppose your program is 10,000 lines long and written in C. Then you have like about 100 lines of where you need to do this struct-punning. In that case, it's probably worth it to use a bit more effort to keep the entire program compatible in C.
    – Mysticial
    Sep 5, 2011 at 21:04

3 Answers 3

41

Yes, the C standard specifically guarantees that this will work.

(C1x §6.7.2.1.13: "A pointer to a structure object, suitably converted, points to its initial member ... and vice versa. There may be unnamed padding within as structure object, but not at its beginning.")

2
2

The whole gtk+ is implemented like that. I cannot think of a better example. Take a look at http://git.gnome.org/browse/gtk+/tree/gtk/

1

I'm not disagreeing with any of the answers saying that what you suggested will work, but in the interests of a more complete discussion (without suggesting you use C++!), why not do something like

typedef struct Base ...  /* The types defined exactly as before */
typedef struct Foo ...
typedef struct Bar ...

/* The function takes a Base* since that is what it actually works with*/
void MethodOperatesOnBase(Base* pbase)
{
    /* Do something... */
}

/* Now call it like this: */
Foo foo;
Bar bar;

MethodOperatesOnBase(&foo.base);
MethodOperatesOnBase(&bar.base);

Is there some reason that won't work and you need to use void *? I don't see that this is much more work and it does have the advantage of type-safety.

2
  • Well, In my particular case, the reason is because Foo and Bar are being exposed as opaque pointers.
    – Steve
    Sep 5, 2011 at 21:39
  • 1
    @ Steve: OK, so the caller isn't able to make the reference to the base member and you have to do it by reinterpreting the struct pointer. That makes sense, thanks for clarifying.
    – AAT
    Sep 5, 2011 at 21:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.