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/t956713-re-struct-assignment.html)

Edward A. Falk 01-19-2013 11:41 PM

Re: Struct assignment
 
In article <u0fqs9-929.ln1@main.anatron.com.au>,
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, falk@despams.r.us.com
http://thespamdiaries.blogspot.com/

glen herrmannsfeldt 01-20-2013 01:01 AM

Re: Struct assignment
 
Edward A. Falk <falk@rahul.net> wrote:
> In article <u0fqs9-929.ln1@main.anatron.com.au>,
> 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

James Kuyper 01-20-2013 01:53 AM

Re: Struct assignment
 
On 01/19/2013 06:41 PM, Edward A. Falk wrote:
> In article <u0fqs9-929.ln1@main.anatron.com.au>,
> 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 01-20-2013 01:53 AM

Re: Struct assignment
 
On 01/19/2013 06:41 PM, Edward A. Falk wrote:
> In article <u0fqs9-929.ln1@main.anatron.com.au>,
> 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

Tim Rentsch 01-20-2013 08:03 PM

Re: Struct assignment
 
James Kuyper <jameskuyper@verizon.net> writes:

> On 01/19/2013 06:41 PM, Edward A. Falk wrote:
>> In article <u0fqs9-929.ln1@main.anatron.com.au>,
>> 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.


All times are GMT. The time now is 12:33 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