Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Structure without a tag |
| Date | 2011-10-15 17:16 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <j7ctbe$pp4$1@dont-email.me> (permalink) |
| References | <ea62ba19-4091-4bf8-992c-61676bfcd36a@ff5g2000vbb.googlegroups.com> |
On 10/15/2011 4:20 PM, Максим Фомин wrote:
> Consider this code:
>
> struct
> {
> int x;
> int y;
> } one;
>
> struct
> {
> int x;
> int y;
> } two;
>
> int main(void)
> {
> one.x = one.y = 6;
> one = two;
> return 0;
> }
>
> Compiler rejects assignment of first structure to second
> ("incompatible types when assigning to type ‘struct<anonymous>’ from
> type ‘struct<anonymous>’").
>
> It seems to me that objects one and two have type struct {int x; int
> y} and therefore their types are equal.
>
> If not, which type they have?
The language Standard states the rules for "type compatibility,"
and according to those rules the types of `one' and `two' are not
compatible. Yes, they look alike. But they are not compatible, and
you cannot assign from one to the other.
This is usually a Good Thing, because the fact that the two types
have the same representation is just a coincidence. There are many
different kinds of things you might represent with a pair of ints:
a point in a two-dimensional grid, the scores of each side in a
football match, the number of votes for and against a proposed law,
the number of axles and weight in tonnes of a truck, ... Even though
both representations consist of two ints, it would probably be a mistake
to store a vote tally in the description of a truck!
Or, consider
#include <limits.h>
struct {
int x, y;
} one;
struct {
#if INT_MAX >= 1000000
int x, y;
#else
long x, y;
#endif
} two;
Depending on the platform, `one' and `two' might or might not have
the same representation. If the representations happen to agree on
Platform X, do you think the assignment `one = two' is a good idea?
The programmer quite clearly has different expectations about what
can be stored in `one.x' and `two.x', even if the representations
sometimes turn out to be the same.
--
Eric Sosman
esosman@ieee-dot-org.invalid
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Structure without a tag Максим Фомин <maxim@maxim-fomin.ru> - 2011-10-15 13:20 -0700
Re: Structure without a tag Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-10-15 17:16 -0400
Re: Structure without a tag Frederick Williams <freddywilliams@btinternet.com> - 2011-10-16 15:19 +0100
Re: Structure without a tag Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-10-16 10:30 -0400
Re: Structure without a tag Keith Thompson <kst-u@mib.org> - 2011-10-16 13:54 -0700
Re: Structure without a tag Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-10-16 22:09 +0000
Re: Structure without a tag Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-10-16 18:29 -0400
Re: Structure without a tag "BartC" <bc@freeuk.com> - 2011-10-16 23:55 +0100
Re: Structure without a tag blp@cs.stanford.edu (Ben Pfaff) - 2011-10-16 17:31 -0700
Re: Structure without a tag James Kuyper <jameskuyper@verizon.net> - 2011-10-16 20:05 -0400
Re: Structure without a tag Kaz Kylheku <kaz@kylheku.com> - 2011-10-17 17:02 +0000
Re: Structure without a tag James Kuyper <jameskuyper@verizon.net> - 2011-10-17 13:53 -0400
Re: Structure without a tag Robert Wessel <robertwessel2@yahoo.com> - 2011-10-17 13:38 -0500
Re: Structure without a tag Максим Фомин <maxim@maxim-fomin.ru> - 2011-10-17 22:28 -0700
Re: Structure without a tag gordonb.b1148@burditt.org (Gordon Burditt) - 2011-10-18 03:04 -0500
Re: Structure without a tag Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-10-18 01:37 -0700
Re: Structure without a tag James Kuyper <jameskuyper@verizon.net> - 2011-10-18 07:10 -0400
Re: Structure without a tag gwowen <gwowen@gmail.com> - 2011-10-18 08:43 -0700
Re: Structure without a tag James Kuyper <jameskuyper@verizon.net> - 2011-10-18 11:55 -0400
Re: Structure without a tag "BartC" <bc@freeuk.com> - 2011-10-18 17:18 +0100
Re: Structure without a tag Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-10-18 15:09 -0600
Re: Structure without a tag Patrick Scheible <kkt@zipcon.net> - 2011-10-18 08:59 -0700
Re: Structure without a tag James Kuyper <jameskuyper@verizon.net> - 2011-10-18 12:54 -0400
Attributions (was Re: Structure without a tag) Keith Thompson <kst-u@mib.org> - 2011-10-18 11:48 -0700
Re: Structure without a tag Stephen Sprunk <stephen@sprunk.org> - 2011-10-18 16:20 -0500
Re: Structure without a tag Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-24 18:05 -0800
Re: Structure without a tag Keith Thompson <kst-u@mib.org> - 2011-10-15 16:30 -0700
Re: Structure without a tag Jean-Christophe <5.d@free.fr> - 2011-10-16 09:54 -0700
csiph-web