Re: Struct assignment

Discussion in 'C Programming' started by glen herrmannsfeldt, Jan 19, 2013.

  1. 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>'


    Does the problem go away if you name the structs?
    (different names)

    -- glen
     
    glen herrmannsfeldt, Jan 19, 2013
    #1
    1. Advertising

  2. glen herrmannsfeldt

    James Kuyper Guest

    On 01/19/2013 02:18 PM, glen herrmannsfeldt wrote:
    > 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>'

    >
    > Does the problem go away if you name the structs?
    > (different names)


    The two struct types remain incompatible even if named.
    --
    James Kuyper
     
    James Kuyper, Jan 19, 2013
    #2
    1. Advertising

  3. glen herrmannsfeldt

    Shao Miller Guest

    On 1/19/2013 15:22, James Kuyper wrote:
    > On 01/19/2013 02:18 PM, glen herrmannsfeldt wrote:
    >> 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>'

    >>
    >> Does the problem go away if you name the structs?
    >> (different names)

    >
    > The two struct types remain incompatible even if named.
    >


    And the confusion about their compatibility goes away, which is the
    problem, here.

    --
    - Shao Miller
    --
    "Thank you for the kind words; those are the kind of words I like to hear.

    Cheerily," -- Richard Harter
     
    Shao Miller, Jan 19, 2013
    #3
  4. glen herrmannsfeldt

    Eric Sosman Guest

    On 1/19/2013 3:22 PM, James Kuyper wrote:
    > On 01/19/2013 02:18 PM, glen herrmannsfeldt wrote:
    >> 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>'

    >>
    >> Does the problem go away if you name the structs?
    >> (different names)

    >
    > The two struct types remain incompatible even if named.


    Brevity is the soul of wit, but perhaps that's just a little
    bit *too* witty ...

    If the two declarations are given different struct tags

    struct mutt { int a; } sa;
    struct jeff { int a; } sb;

    the two types are still incompatible, maybe more obviously so.

    If the two declarations are given the same struct tag

    struct mutt { int a; } sa;
    struct mutt { int a; } sb;

    there's only one `struct mutt' type, but it's declared more than
    once -- a different kind of no-no. The same problem crops up
    when using a typedef

    typedef struct { int a; } dilbert;
    typedef struct { int a; } dilbert;
    dilbert sa;
    dilbert sb;

    except here it's the `dilbert' identifier that's doubly defined
    (incompatibly, too).

    A couple of correct ways to declare two variables of the
    same struct type:

    struct mutt { int a; } sa;
    struct mutt sb;

    struct mutt { int a; };
    struct mutt sa;
    struct mutt sb;

    typedef struct { int a; } dilbert;
    dilbert sa, sb;

    struct { int a; } sa, sb;

    In the last example, `sa' and `sb' are the only variables of the
    anonymous type; it's not possible to declare any others once
    the `;' is in the rear-view mirror.

    --
    Eric Sosman
    d
     
    Eric Sosman, Jan 19, 2013
    #4
  5. glen herrmannsfeldt

    Tim Rentsch Guest

    Eric Sosman <> writes:

    > [snip]
    >
    > If the two declarations are given the same struct tag
    >
    > struct mutt { int a; } sa;
    > struct mutt { int a; } sb;
    >
    > there's only one `struct mutt' type, but it's declared more
    > than once -- a different kind of no-no. [snip]


    What he means is that the contents are defined more than once.
    It is perfectly okay to declare 'struct mutt' multiple times;
    what the Standard prohibits is defining the contents (ie, what's
    between the {}'s) more than once.
     
    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,405
    Chris Fogelklou
    Apr 20, 2004
  2. nagy
    Replies:
    36
    Views:
    1,022
    Terry Reedy
    Jul 20, 2006
  3. Eric Laberge

    Array assignment via struct

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

Share This Page