Re: Struct assignment

Discussion in 'C Programming' started by Edward A. Falk, Jan 19, 2013.

  1. In article <>,
    Russell Shaw <rjshawN_o@s_pam.netspace.net.au> wrote:
    >Hi,
    >In gcc-4.7 C99, i get an error (in a function scope):
    >
    > struct {
    > int a;
    > } sa;
    >
    > struct {
    > int a;
    > } sb;
    >
    > sb = sa;
    >
    >error: incompatible types when assigning to type 'struct <anonymous>' from type
    >'struct <anonymous>'


    You're asking rather a lot for the compiler to recognize that two different
    structures just happen to have been defined the same way.

    Try this:

    struct foo {
    int a;
    };

    struct foo sa;
    struct foo sb;

    sb = sa;

    --
    -Ed Falk,
    http://thespamdiaries.blogspot.com/
     
    Edward A. Falk, Jan 19, 2013
    #1
    1. Advertising

  2. Edward A. Falk <> wrote:
    > In article <>,
    > Russell Shaw <rjshawN_o@s_pam.netspace.net.au> wrote:


    >>In gcc-4.7 C99, i get an error (in a function scope):


    >> struct {
    >> int a;
    >> } sa;


    >> struct {
    >> int a;
    >> } sb;


    >> sb = sa;


    >>error: incompatible types when assigning to type 'struct <anonymous>' from type
    >>'struct <anonymous>'


    > You're asking rather a lot for the compiler to recognize that two different
    > structures just happen to have been defined the same way.


    Well, it doesn't have to 'just' recognize it, but only test when it
    finds an actual operation.

    I believe that PL/I can do it. I am not sure what happens if you give
    PL/I a structure expression of incompatible structures. My guess is
    that it converts as appropriate.

    DCL 1 SA, 2 A FIXED BINARY(31,0);
    DCL 1 SB, 2 A FIXED BINARY(31,0);

    I am pretty sure that PL/I will let you do either

    SA=SB;

    or even

    SA=SA+SB;

    and, for extra challange:

    DCL 1 SC, 2 A FIXED BINARY(31,0);
    DCL 1 SD, 2 A FLOAT BINARY(53);

    SD=SD+SQRT(SC);

    -- glen
     
    glen herrmannsfeldt, Jan 20, 2013
    #2
    1. Advertising

  3. Edward A. Falk

    James Kuyper Guest

    On 01/19/2013 06:41 PM, Edward A. Falk wrote:
    > In article <>,
    > Russell Shaw <rjshawN_o@s_pam.netspace.net.au> wrote:
    >> Hi,
    >> In gcc-4.7 C99, i get an error (in a function scope):
    >>
    >> struct {
    >> int a;
    >> } sa;
    >>
    >> struct {
    >> int a;
    >> } sb;
    >>
    >> sb = sa;
    >>
    >> error: incompatible types when assigning to type 'struct <anonymous>' from type
    >> 'struct <anonymous>'

    >
    > You're asking rather a lot for the compiler to recognize that two different
    > structures just happen to have been defined the same way.


    Perhaps - yet that is precisely what the standard would have required if
    they had been declared in two different translation units; it's only
    because they are in the same translation unit that they're not
    compatible (6.2.7p1).
    --
    James Kuyper
     
    James Kuyper, Jan 20, 2013
    #3
  4. Edward A. Falk

    James Kuyper Guest

    On 01/19/2013 06:41 PM, Edward A. Falk wrote:
    > In article <>,
    > Russell Shaw <rjshawN_o@s_pam.netspace.net.au> wrote:
    >> Hi,
    >> In gcc-4.7 C99, i get an error (in a function scope):
    >>
    >> struct {
    >> int a;
    >> } sa;
    >>
    >> struct {
    >> int a;
    >> } sb;
    >>
    >> sb = sa;
    >>
    >> error: incompatible types when assigning to type 'struct <anonymous>' from type
    >> 'struct <anonymous>'

    >
    > You're asking rather a lot for the compiler to recognize that two different
    > structures just happen to have been defined the same way.


    Perhaps - yet that is precisely what the standard would have required if
    they had been declared in two different translation units; it's only
    because they are in the same translation unit that they're not
    compatible (6.2.7p1).
    --
    James Kuyper
     
    James Kuyper, Jan 20, 2013
    #4
  5. Edward A. Falk

    Tim Rentsch Guest

    James Kuyper <> writes:

    > On 01/19/2013 06:41 PM, Edward A. Falk wrote:
    >> In article <>,
    >> Russell Shaw <rjshawN_o@s_pam.netspace.net.au> wrote:
    >>> Hi,
    >>> In gcc-4.7 C99, i get an error (in a function scope):
    >>>
    >>> struct {
    >>> int a;
    >>> } sa;
    >>>
    >>> struct {
    >>> int a;
    >>> } sb;
    >>>
    >>> sb = sa;
    >>>
    >>> error: incompatible types when assigning to type 'struct
    >>> <anonymous>' from type 'struct <anonymous>'

    >>
    >> You're asking rather a lot for the compiler to recognize that
    >> two different structures just happen to have been defined the
    >> same way.

    >
    > Perhaps - yet that is precisely what the standard would have
    > required if they had been declared in two different translation
    > units; [snip]


    I expect M. Kuyper means something different from what he is
    saying. C compilers don't even try to determine whether structs
    declared in different translation units are defined the same way,
    and certainly the Standard doesn't require them to do so.
     
    Tim Rentsch, Jan 20, 2013
    #5
    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. Chris Fogelklou
    Replies:
    36
    Views:
    1,391
    Chris Fogelklou
    Apr 20, 2004
  2. nagy
    Replies:
    36
    Views:
    1,011
    Terry Reedy
    Jul 20, 2006
  3. Eric Laberge

    Array assignment via struct

    Eric Laberge, Aug 4, 2005, in forum: C Programming
    Replies:
    36
    Views:
    920
    Dave Thompson
    Aug 14, 2005
  4. Chris
    Replies:
    34
    Views:
    1,526
  5. Noob
    Replies:
    25
    Views:
    1,478
    Nick Keighley
    Dec 9, 2009
Loading...

Share This Page