casting of structs

Discussion in 'C Programming' started by Servé Lau, Dec 26, 2003.

  1. Servé Lau

    Servé Lau Guest

    Consider the following code:

    struct X { float f; };
    struct Y { struct X x; };

    void func(struct X *x) {}

    int main(void) {
    struct Y y;
    func(&y);

    return 0;
    }

    Ok, there's two structs. One struct X with some member and struct Y that has
    a struct X as the first member. In OO terms we say that Y inherits from X.
    Therefore, struct Y can safely be used as a struct X right? Is the above
    legal code, can we call func() without casting Y * to X *?
    My compiler accepts it without warning and I think it should.
    Servé Lau, Dec 26, 2003
    #1
    1. Advertising

  2. "Servé Lau" <> spoke thus:

    > struct X { float f; };
    > struct Y { struct X x; };
    > (etc.)


    > My compiler accepts it without warning and I think it should.


    cat b.c

    struct X { float f; };
    struct Y { struct X x; };

    void func(struct X *x) {}

    int main(void) {
    struct Y y;
    func(&y);

    return 0;
    }

    gcc -Wall -pedantic -O2 -ansi b.c

    b.c: In function `main':
    b.c:8: warning: passing arg 1 of `func' from incompatible pointer type

    I don't think your compiler is doing you any favors by accepting your
    code without issuing a diagnostic.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Dec 26, 2003
    #2
    1. Advertising

  3. Servé Lau

    Servé Lau Guest

    "Christopher Benson-Manica" <> wrote in message
    news:bsi3id$ghq$...
    > I don't think your compiler is doing you any favors by accepting your
    > code without issuing a diagnostic.


    Why not? Is it not true that all pointers to Y can be used as a pointer to
    X?
    Servé Lau, Dec 26, 2003
    #3
  4. "Servé Lau" <> wrote in message
    news:bsicc6$8ak$1.nb.home.nl...

    In the context of...

    > struct X { float f; };
    > struct Y { struct X x; };



    > "Christopher Benson-Manica" <> wrote in message
    > news:bsi3id$ghq$...
    > > I don't think your compiler is doing you any favors by accepting your
    > > code without issuing a diagnostic.

    >
    > Why not? Is it not true that all pointers to Y can be used as a pointer to
    > X?


    No. A pointer to a structure can be portably /converted/ to a pointer to the
    first element of the structure and back. Please note the word 'converted'.
    On most PC-based platforms, the pointers will even be binary equivalent, but
    it does not necessarily have to be the case.

    If you want to simulate OOP inheritance in C, do something like:

    In the context of...

    struct X { /* fileds */ };
    struct Y { struct X x; /* more fields */ };
    ....
    type function (struct X *);
    ....
    int main (void)
    {
    struct Y y;
    func(&y.x); /* note .x */
    return 0;
    }
    Peter Pichler, Dec 26, 2003
    #4
  5. Servé Lau

    Chris Torek Guest

    In article <bsi33h$21t$1.nb.home.nl>,
    Servé Lau <> wrote:
    [one "struct" contains another whole, so that &y.x has type "struct X *",
    and:]

    >void func(struct X *x) {}
    >
    >int main(void) {
    > struct Y y;
    > func(&y);
    >
    > return 0;
    >}
    >
    >Ok, there's two structs. One struct X with some member and struct Y that has
    >a struct X as the first member. In OO terms we say that Y inherits from X.


    In "proper" OO terms the member named y.x (of type "struct X")
    should be able to go anywhere, not just first.

    >Therefore, struct Y can safely be used as a struct X right?


    In C89 and C99, only via conversions.

    >Is the above legal code, can we call func() without casting Y * to X *?
    >My compiler accepts it without warning and I think it should.


    C89 and C99 both require diagnostics.

    Plan 9 C, which is a different language from both C89 and C99,
    allows this kind of call. It works even if "y.x" is not the
    first member, too -- the call has the same effect as func(&y.x).
    However, the definition for struct Y must read rather differently:

    struct Y {
    int any, stuff, you, like;
    struct X; /* inherit all of struct X's members */
    int more, things, ifdesired;
    };

    I am not quite sure what Plan 9 C says must occur if "struct X"
    has members whose names conflict with those of "struct Y" (exclusive
    of "struct X" of course). Moreover, what does it mean if you
    try to inherit from the same datatype more than once? For
    instance:

    struct Point { int x, y; };

    struct Rectangle {
    Point; /* e.g., upper left corner */
    int w, h; /* width and height */
    };

    is OK, but what about:

    struct Rectangle {
    Point;
    double x; /* error? ok? */
    };

    and clearly:

    struct Rectangle {
    Point; /* upper left */
    Point; /* lower right */
    };

    is right out. :)
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.
    Chris Torek, Dec 27, 2003
    #5
  6. Servé Lau

    Simon Biber Guest

    "Servé Lau" <> wrote:
    > Consider the following code:
    >
    > struct X { float f; };
    > struct Y { struct X x; };
    >
    > void func(struct X *x) {}
    >
    > int main(void) {
    > struct Y y;
    > func(&y);


    This line is a constraint violation, a conforming compiler must
    issue a diagnostic for the code. For example:
    slau.c:8: warning: passing arg 1 of `func' from incompatible pointer type

    > return 0;
    > }


    > Ok, there's two structs. One struct X with some member and struct Y
    > that has a struct X as the first member. In OO terms we say that Y
    > inherits from X. Therefore, struct Y can safely be used as a struct
    > X right?


    Yes, it can, so long as you convert the pointer properly.

    > Is the above legal code, can we call func() without casting
    > Y * to X *? My compiler accepts it without warning and I think
    > it should.


    No, you must use a cast to convert from Y* to X*. Any compiler that
    accepts it without a diagnostic (either warning or error) is not
    a standard-conforming compiler or not being invoked with the right
    options (such as GCC's -ansi -pedantic).

    --
    Simon.
    Simon Biber, Dec 27, 2003
    #6
  7. Servé Lau

    Jack Klein Guest

    On Fri, 26 Dec 2003 20:44:59 +0100, "Servé Lau" <>
    wrote in comp.lang.c:

    > Consider the following code:
    >
    > struct X { float f; };
    > struct Y { struct X x; };
    >
    > void func(struct X *x) {}
    >
    > int main(void) {
    > struct Y y;
    > func(&y);
    >
    > return 0;
    > }
    >
    > Ok, there's two structs. One struct X with some member and struct Y that has
    > a struct X as the first member. In OO terms we say that Y inherits from X.


    There are no OO terms in C.

    > Therefore, struct Y can safely be used as a struct X right? Is the above
    > legal code, can we call func() without casting Y * to X *?
    > My compiler accepts it without warning and I think it should.


    Either your compiler is broken, or you are using it in a
    non-conforming mode.

    C is a typed language. Regardless of location in memory, a pointer to
    a struct X is not a pointer to a struct Y or a pointer to a float.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
    Jack Klein, Dec 27, 2003
    #7
  8. Peter Pichler <> spoke thus:

    > No. A pointer to a structure can be portably /converted/ to a pointer to the
    > first element of the structure and back.


    FMI, can you quote the portion of the Standard that allows this?
    Consider my curiousity piqued :)

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Dec 29, 2003
    #8
  9. Christopher Benson-Manica wrote :
    > Peter Pichler spoke thus:
    >
    > > No. A pointer to a structure can be portably /converted/ to a pointer to

    the
    > > first element of the structure and back.

    >
    > FMI, can you quote the portion of the Standard that allows this?
    > Consider my curiousity piqued :)


    Ehm, did I manage to make a fool of myself /again/? ;-)

    No quote, just my simplified interpretation of 6.7.2.1:
    ....
    13 Within a structure object, the non-bit-field members and the units in
    which bit-fields reside have addresses that increase in the order in
    which they are declared. A pointer to a structure object, suitably
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    converted, points to its initial member (or if that member is a
    bit-field,
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    then to the unit in which it resides), and vice versa. There may be
    ^^^^^^^^^^^^^^
    unnamed padding within a structure object, but not at its beginning.
    Peter Pichler, Dec 29, 2003
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patricia  Van Hise

    structs with fields that are structs

    Patricia Van Hise, Apr 5, 2004, in forum: C Programming
    Replies:
    5
    Views:
    621
    Al Bowers
    Apr 5, 2004
  2. Chris Hauxwell

    const structs in other structs

    Chris Hauxwell, Apr 23, 2004, in forum: C Programming
    Replies:
    6
    Views:
    547
    Chris Hauxwell
    Apr 27, 2004
  3. Paminu
    Replies:
    5
    Views:
    630
    Eric Sosman
    Oct 11, 2005
  4. Daniel Rudy
    Replies:
    15
    Views:
    1,370
    Keith Thompson
    Apr 10, 2006
  5. Tuan  Bui
    Replies:
    14
    Views:
    462
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page