Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Re: Struct assignment (http://www.velocityreviews.com/forums/t956705-re-struct-assignment.html)

glen herrmannsfeldt 01-19-2013 07:18 PM

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

James Kuyper 01-19-2013 08:22 PM

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

Shao Miller 01-19-2013 08:47 PM

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

Eric Sosman 01-19-2013 10:56 PM

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

Tim Rentsch 01-20-2013 08:12 PM

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.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57