![]() |
Re: Struct assignment
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 |
Re: Struct assignment
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 |
Re: Struct assignment
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 |
Re: Struct assignment
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 esosman@comcast-dot-net.invalid |
Re: Struct assignment
Eric Sosman <esosman@comcast-dot-net.invalid> 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. |
| All times are GMT. The time now is 02:40 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.