Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: Struct assignment

Reply
Thread Tools

Re: Struct assignment

 
 
Edward A. Falk
Guest
Posts: n/a
 
      01-19-2013
In article <(E-Mail Removed)>,
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://www.velocityreviews.com/forums/(E-Mail Removed)
http://thespamdiaries.blogspot.com/
 
Reply With Quote
 
 
 
 
glen herrmannsfeldt
Guest
Posts: n/a
 
      01-20-2013
Edward A. Falk <(E-Mail Removed)> wrote:
> In article <(E-Mail Removed)>,
> 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
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      01-20-2013
On 01/19/2013 06:41 PM, Edward A. Falk wrote:
> In article <(E-Mail Removed)>,
> 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
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      01-20-2013
On 01/19/2013 06:41 PM, Edward A. Falk wrote:
> In article <(E-Mail Removed)>,
> 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
 
Reply With Quote
 
Tim Rentsch
Guest
Posts: n/a
 
      01-20-2013
James Kuyper <(E-Mail Removed)> writes:

> On 01/19/2013 06:41 PM, Edward A. Falk wrote:
>> In article <(E-Mail Removed)>,
>> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
Rationale for struct assignment and no struct comparison Noob C Programming 25 12-09-2009 08:56 AM
Assignment operator self-assignment check Chris C++ 34 09-26-2006 04:26 AM
Augument assignment versus regular assignment nagy Python 36 07-20-2006 07:24 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments