Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #27552

Re: Compatibility question

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Compatibility question
Date 2012-10-22 16:50 -0700
Organization None to speak of
Message-ID <lnbofuf4mn.fsf@nuthaus.mib.org> (permalink)
References (6 earlier) <JLOhFS.Dvd.FY9Iy@gmail.com> <0.d779297035d2edccd1b4.20121022173052BST.87ehkqbhab.fsf@bsb.me.uk> <2nYjPo.Krd.2aRbe@gmail.com> <lnfw56fl74.fsf@nuthaus.mib.org> <qRFu8I.lcH.7XEwQ@gmail.com>

Show all headers | View raw


Steve Thompson <stevet810@gmail.com> writes:
> On Mon, Oct 22, 2012 at 10:52:47AM -0700, Keith Thompson wrote:
>> Steve Thompson <stevet810@gmail.com> writes:
>> [...]
>> > No annoyance taken, although now that I think of it it does show an
>> > instance of overloaded semantics, which can be confusing.  When I was
>> > less experienced with C, I was confused by the fact that variables
>> > defined as structures were not pointers:
>> >
>> > struct point {
>> > 	int x, y, z;
>> > };
>> >
>> > struct point a;
>> >
>> >
>> > One cannot reference 'a' (as such) in an expression even though it is
>> > a pointer in the implementation because C allows one to use structures
>> > as arguments to a function.  I'd never do that in my own code, but
>> > some people may like the fact that they can pass complex data
>> > structures around as if they were simple variables.  I think it wastes
>> > resources, but of course that is less of an issue today than it was
>> > when C was first developed.
>> 
>> I'm not sure what you mean when you say `a` "is a pointer in
>> the implementation", especially after you mention "the fact that
>> variables defined as structures were not pointers".
>
> Meaning that under the hood in the C compiler, 'a' is really a
> pointer.  The logic of the compiler causes it to use the structure
> data as specified in the standard, but conceptually 'a' is still a
> pointer to the data in the specific structure identified by 'a'.  I
> found it very confusing when I was learning C.

Frankly, I think you're still confused.

`a` is not in any sense a pointer.  It's one of several things,
depending on what level of abstraction you're looking at:

- An identifier that's the name of an object of type `struct point`;
- An object of type `struct point`;
- An lvalue expression that designates an object of type `struct point`;
- A non-lvalue expression that evaluates to the value of an object of
  type `struct point`, a value that consists of the values of the
  members.

If you want a pointer to `a`, you can construct one easily enough
by writing `&a`.  A compiler *might* use pointers internally to
handle some operations on `a`, but there's no reason at all to
assume that it has to.  For example, here's the code generated by
gcc for an assignment `b = a;`, where `b` and `a` are both of type
`struct point`:

        movl    56(%esp), %eax
        movl    %eax, 68(%esp)
        movl    60(%esp), %eax
        movl    %eax, 72(%esp)
        movl    64(%esp), %eax
        movl    %eax, 76(%esp)

And here's the code generated for an assignment `y = x;`, where `y` and
`x` are of type `long double`:

        movl    16(%esp), %eax
        movl    20(%esp), %edx
        movl    24(%esp), %ecx
        movl    %eax, 32(%esp)
        movl    %edx, 36(%esp)
        movl    %ecx, 40(%esp)

Looks pretty similar to me.  Is a long double object "really" a pointer?

[...]

> True enough, but the difference is that a structure cannot be used for
> arithmetic operations of any kind in C.  That makes it fundamentally
> different from simple type variables.  It may be meaningful to say that
> structures passed to functions as values is a hack in the C language.
> It fills a use-case scenario that will be found (or should be found)
> only in trivial programs, IMHO.

Ok, you can't do arithmetic on structures.  You can't use "%" on
floating-point types.  You *can* assign structures and pass them as
function arguments, with exactly the same syntax and semantics as the
corresponding operations on scalar types.

[...]

There's a close relationship between arrays and pointers (though it's
important to remember that *arrays are not pointers*; see section 6 of
the comp.lang.c FAQ <http://www.c-faq.com>).  There is no such special
relationship at the language or compiler level between structures and
pointers.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-20 03:08 +0000
  Re: Compatibility question Ian Collins <ian-news@hotmail.com> - 2012-10-20 16:20 +1300
    Re: Compatibility question Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-10-20 11:50 +0100
      Re: Compatibility question Keith Thompson <kst-u@mib.org> - 2012-10-20 11:41 -0700
        Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-21 23:04 +0000
          Re: Compatibility question Barry Schwarz <schwarzb@dqel.com> - 2012-10-21 20:27 -0700
    Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-20 04:01 +0000
      Re: Compatibility question Ian Collins <ian-news@hotmail.com> - 2012-10-21 09:26 +1300
        Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-21 22:54 +0000
          Re: Compatibility question Ian Collins <ian-news@hotmail.com> - 2012-10-22 12:29 +1300
            Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-21 23:50 +0000
              Re: Compatibility question Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-10-22 02:27 +0100
                Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-22 02:28 +0000
                Re: Compatibility question Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-10-22 11:18 -0400
                Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-22 16:19 +0000
                Re: Compatibility question Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-10-22 17:37 +0100
                Re: Compatibility question Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-10-22 17:40 +0100
                Re: Compatibility question Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-10-22 17:30 +0100
                Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-22 17:23 +0000
                Re: Compatibility question Keith Thompson <kst-u@mib.org> - 2012-10-22 10:52 -0700
                Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-22 18:42 +0000
                Re: Compatibility question Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-10-22 21:06 +0100
                Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-22 22:52 +0000
                Re: Compatibility question Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-10-23 03:33 +0100
                Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-23 12:28 +0000
                Re: Compatibility question "BartC" <bc@freeuk.com> - 2012-10-23 16:45 +0100
                Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-23 17:53 +0000
                Re: Compatibility question Keith Thompson <kst-u@mib.org> - 2012-10-23 12:41 -0700
                Re: Compatibility question "BartC" <bc@freeuk.com> - 2012-10-23 21:28 +0100
                Re: Compatibility question Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-10-23 20:57 +0100
                Re: Compatibility question Keith Thompson <kst-u@mib.org> - 2012-10-23 12:26 -0700
                Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-23 20:39 +0000
                Re: Compatibility question Keith Thompson <kst-u@mib.org> - 2012-10-23 15:53 -0700
                Re: Compatibility question "BartC" <bc@freeuk.com> - 2012-10-24 00:57 +0100
                Re: Compatibility question Keith Thompson <kst-u@mib.org> - 2012-10-23 20:06 -0700
                Re: Compatibility question "BartC" <bc@freeuk.com> - 2012-10-24 11:18 +0100
                Re: Compatibility question Keith Thompson <kst-u@mib.org> - 2012-10-24 12:04 -0700
                Re: Compatibility question "BartC" <bc@freeuk.com> - 2012-10-24 23:10 +0100
                Re: Compatibility question Keith Thompson <kst-u@mib.org> - 2012-10-24 16:58 -0700
                Re: Compatibility question "BartC" <bc@freeuk.com> - 2012-10-25 01:46 +0100
                Re: Compatibility question Keith Thompson <kst-u@mib.org> - 2012-10-24 19:40 -0700
                Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-24 20:20 +0000
                Re: Compatibility question Keith Thompson <kst-u@mib.org> - 2012-10-24 14:38 -0700
                Re: Compatibility question James Kuyper <jameskuyper@verizon.net> - 2012-10-24 17:55 -0400
                Re: Compatibility question Keith Thompson <kst-u@mib.org> - 2012-10-24 16:29 -0700
                Re: Compatibility question Greg Martin <greg@softsprocket.com> - 2012-10-24 15:21 -0700
                Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-26 03:30 +0000
                Re: Compatibility question Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-10-26 13:02 +0100
                Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-27 00:04 +0000
                Re: Compatibility question Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-10-27 20:15 +0100
                Re: Compatibility question Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2012-10-27 16:36 -0600
                Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-28 17:36 +0000
                Re: Compatibility question Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-10-27 22:42 +0300
                Re: Compatibility question Greg Martin <greg@softsprocket.com> - 2012-10-27 14:30 -0700
                Re: Compatibility question "BartC" <bc@freeuk.com> - 2012-10-27 22:57 +0100
                Re: Compatibility question Keith Thompson <kst-u@mib.org> - 2012-10-27 17:04 -0700
                Re: Compatibility question Keith Thompson <kst-u@mib.org> - 2012-10-27 17:14 -0700
                Re: Compatibility question Keith Thompson <kst-u@mib.org> - 2012-10-27 18:04 -0700
                Re: Compatibility question Keith Thompson <kst-u@mib.org> - 2012-10-24 16:46 -0700
                Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-25 01:18 +0000
                Re: Compatibility question Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-10-27 21:08 +0300
                Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-28 17:38 +0000
                Re: Compatibility question "BartC" <bc@freeuk.com> - 2012-10-23 00:38 +0100
                Re: Compatibility question Keith Thompson <kst-u@mib.org> - 2012-10-22 16:50 -0700
                Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-23 12:33 +0000
                Re: Compatibility question Öö Tiib <ootiib@hot.ee> - 2012-10-22 14:23 -0700
                Re: Compatibility question James Kuyper <jameskuyper@verizon.net> - 2012-10-22 17:26 -0400
                Re: Compatibility question Ian Collins <ian-news@hotmail.com> - 2012-10-23 10:55 +1300
                Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-22 23:27 +0000
                Re: Compatibility question "BartC" <bc@freeuk.com> - 2012-10-22 22:44 +0100
                Re: Compatibility question Les Cargill <lcargill99@comcast.com> - 2012-10-22 17:15 -0500
                Re: Compatibility question "BartC" <bc@freeuk.com> - 2012-10-23 00:13 +0100
                Re: Compatibility question James Kuyper <jameskuyper@verizon.net> - 2012-10-22 18:35 -0400
                Re: Compatibility question "BartC" <bc@freeuk.com> - 2012-10-23 00:14 +0100
                Re: Compatibility question Steve Thompson <stevet810@gmail.com> - 2012-10-22 23:42 +0000
                Re: Compatibility question Ian Collins <ian-news@hotmail.com> - 2012-10-23 13:35 +1300
                Re: Compatibility question Keith Thompson <kst-u@mib.org> - 2012-10-22 17:01 -0700
                Re: Compatibility question "BartC" <bc@freeuk.com> - 2012-10-23 01:47 +0100
              Re: Compatibility question Keith Thompson <kst-u@mib.org> - 2012-10-21 19:27 -0700

csiph-web