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


Groups > comp.lang.c > #15053

Re: Why C has Pointers.

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Why C has Pointers.
Date 2011-12-27 15:08 -0800
Organization None to speak of
Message-ID <ln4nwl1ulm.fsf@nuthaus.mib.org> (permalink)
References (5 earlier) <0nbqe7p03fis0cbr7l4q0v3nbkli0ql6am@4ax.com> <0.cf93faba882b86d069dd.20111218012659GMT.87iplellek.fsf@bsb.me.uk> <7dqqe7tcsgq8eupkkgaa4eipvu4v504ce5@4ax.com> <ln62hd4md8.fsf@nuthaus.mib.org> <lvckf7d316j2ac8nte36rr00m14vocv7sm@4ax.com>

Show all headers | View raw


ralph <nt_consulting64@yahoo.net> writes:
> On Sun, 18 Dec 2011 13:10:27 -0800, Keith Thompson <kst-u@mib.org>
> wrote:
>>ralph <nt_consulting64@yahoo.net> writes:
>>> On Sun, 18 Dec 2011 01:26:59 +0000, Ben Bacarisse
>>> <ben.usenet@bsb.me.uk> wrote:
>>[...]
>>>>It's not easy to explain the lower-level arithmetic without reference to
>>>>some particular machine.  For example, on a machine with, say, byte and
>>>>word addresses, you can't describe it as you have done.  By all means,
>>>>take a simple architecture and explain what a[0] means on one particular
>>>>system using addresses and size calculations, but you will also have to
>>>>explain, eventually, that arr[i] is, by definition, *(arr + i).
>>>>
>>>
>>> Sure it is because it ain't *(arr + i). It is ...
>>
>>Yes, it is.  C99 6.5.2.1p2:
>>
>>    The definition of the subscript operator [] is that E1[E2] is
>>    identical to (*((E1)+(E2))).
>>
>>> *(arr + ( i * sizeof(datatype)))
>>>
>>> So for int arr[10];
>>> arr[5] is *(arr + (5 * sizeof(int)))
>>> byte arr[10];
>>> arr[5] is *(arr + (5 * sizeof(byte)))
>>
>>You are using your own invented "+" operator that works in units of
>>byte, rather than the actual C "+" operator that works in units of the
>>size of the pointed-to type.  That's fine, but only if you explicitly
>>state that "+" doesn't mean what it means in C.
>
> [So an 'actual' unit of "size of the pointed-to-type" doesn't have the
> same meaning as an 'invented' unit of "sizeof(<declared>type))".

I don't understand what you mean by that statement.  The difference
I was pointing out was between your invented "+" operator, which
apparently operates in units of single bytes (thus the need to
explicitly scale it by sizeof(datatype)), vs. the actual C "+"
operator, which operates in terms of the size of the pointed-to type
(so explicit scaling would yield an incorrect result).

>                                                                  If
> you look closer you will will see there is not a single pointer in my
> statement nor in the Standard - there ain't NO "pointed-to-type" and
> that is the whole point. <bg>]

"arr" is an expression of array type.  In this context, it's implicitly
converted to a pointer to the array's first element.  That's one pointer
right there.

When you add an integer value to a pointer, the result is another
pointer.

The pointed-to type is the element type of "arr", which you refer to as
"datatype".

The expression is a syntactically valid C expression.  It uses
parentheses, names which presumably refer to C objects and type,
the unary and binary "*" operator, the "+" operator, and the
"sizeof" operator.  It would be unreasonable, particularly in the
context of this newsgroup or a C class, to assume that it's *not*
intended to be a C expression with C semantics.

> Sry delay. The holidays...
>
> This reply will have to serve for all of you. <g>
>
> When I mentioned this phenomena as an aid for new programmers, I used
> the term "storage map equation" which I assumed everyone understood as
> an implementation context. My post that prompted this response was
> made quickly because I seriously thought Ben was teasing me. Bad
> assumption. You guys are serious.
>
> Well that is pretty much what you get when you assume. lol
>
> The concepts behind ...
>
>  datatype arr[i]  ~==  *(arr + ( i * sizeof(datatype))) 
>
> ... and understanding the difference between addresses and pointer
> datatypes has served to untangle a ton of "pointer" questions. But
> that's me, and my students, and it doesn't matter. ...

The equation as you wrote it is probably easily understandable
*for someone who doesn't know how C pointer arithmetic works*.
That's what I find dangerous about it.  If you teach this before
you teach C pointer arithmetic, a student who goes back and looks
at this equation is likely to be confused, and/or may misunderstand
the actual rules for C pointer arithmetic when encountering them.

It's possible to express the concept you're presenting (which is
a perfectly valid concept) without violating or ignoring C rules.
You can do in pure C by adding casts to and from char*, but that's
probably too confusing.  Or perhaps you could define and use your
own special-purpose operators that don't look exactly like C's "+",
or you could describe it in English.

I'd actually be interested in seeing a good novice-friendly way of
doing that.

> IF one is pitching something, and no one is catching, especially those
> one definitely respects and believes to be quite competent (and not
> assumed <g>) there is something fishy with the pitch. So I yield.
> Consider item #9 deleted from my list.

I get the impression that you're yielding on this point because of the
number of people who disagree with you.  I'd prefer to think that you
understand the argument we're making.  (If you change your mind, we'll
*all* be right!)

Or maybe I'm just beating a dead horse.

> Happy Holidays and Best Wishes for a Great New Year.

Likewise.

-- 
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

Why C has Pointers. curixinfotech <curixinfotech.54dl3z@no-mx.forums.yourdomain.com.au> - 2011-12-07 07:22 -0500
  Re: Why C has Pointers. Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-12-07 04:35 -0800
  Re: Why C has Pointers. Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-12-07 07:58 -0500
    Re: Why C has Pointers. Mike Manilone <zhtx10@gmail.com> - 2011-12-07 13:15 +0000
      Re: Why C has Pointers. Bill Reid <hormelfree@gmail.com> - 2011-12-08 06:29 -0800
  Re: Why C has Pointers. Vincenzo Mercuri <vincenzo.mercuri@hotmail.it> - 2011-12-07 14:50 +0100
    Re: Why C has Pointers. Keith Thompson <kst-u@mib.org> - 2011-12-07 11:01 -0800
      Re: Why C has Pointers. Vincenzo Mercuri <vincenzo.mercuri@yahoo.it> - 2011-12-07 20:49 +0100
      Re: Why C has Pointers. Brian <nospam@nospam.com> - 2011-12-07 20:00 +0000
        Re: Why C has Pointers. James Kuyper <jameskuyper@verizon.net> - 2011-12-07 15:30 -0500
          Re: Why C has Pointers. 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-07 12:43 -0800
            Re: Why C has Pointers. Keith Thompson <kst-u@mib.org> - 2011-12-07 13:09 -0800
          Re: Why C has Pointers. Vincenzo Mercuri <vincenzo.mercuri@hotmail.it> - 2011-12-08 19:39 +0100
            Re: Why C has Pointers. James Kuyper <jameskuyper@verizon.net> - 2011-12-08 14:16 -0500
              Re: Why C has Pointers. Vincenzo Mercuri <vincenzo.mercuri@hotmail.it> - 2011-12-08 20:23 +0100
    Re: Why C has Pointers. Kaz Kylheku <kaz@kylheku.com> - 2011-12-07 20:48 +0000
      Re: Why C has Pointers. Vincenzo Mercuri <vincenzo.mercuri@yahoo.it> - 2011-12-07 22:07 +0100
      Re: Why C has Pointers. Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-12-10 07:34 +0000
  Re: Why C has Pointers. August Karlstrom <fusionfile@gmail.com> - 2011-12-07 16:16 +0100
  Re: Why C has Pointers. Borax Man <rotflol2@hotmail.com> - 2011-12-08 18:32 +1100
    Re: Why C has Pointers. Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-12-08 00:20 -0800
      Re: Why C has Pointers. André Gillibert <MetaEntropy.removeThis@gmail.com> - 2011-12-08 13:44 +0100
        Re: Why C has Pointers. Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-12-09 05:04 -0800
          Re: Why C has Pointers. gazelle@shell.xmission.com (Kenny McCormack) - 2011-12-09 14:59 +0000
            Re: Why C has Pointers. Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-12-09 20:19 -0800
            Re: Why C has Pointers. Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-12-10 07:36 -0800
  Re: Why C has Pointers. Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-12-08 00:12 -0800
  Re: Why C has Pointers. Cong Wang <xiyou.wangcong@gmail.com> - 2011-12-15 07:26 +0000
  Re: Why C has Pointers. 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-17 08:51 -0800
    Re: Why C has Pointers. Barry Schwarz <schwarzb@dqel.com> - 2011-12-17 10:10 -0800
      Re: Why C has Pointers. ralph <nt_consulting64@yahoo.net> - 2011-12-17 14:28 -0600
        Re: Why C has Pointers. ralph <nt_consulting64@yahoo.net> - 2011-12-17 15:00 -0600
        Re: Why C has Pointers. Keith Thompson <kst-u@mib.org> - 2011-12-17 13:55 -0800
          Re: Why C has Pointers. ralph <nt_consulting64@yahoo.net> - 2011-12-17 18:27 -0600
            Re: Why C has Pointers. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-12-18 01:26 +0000
              Re: Why C has Pointers. ralph <nt_consulting64@yahoo.net> - 2011-12-17 22:34 -0600
                Re: Why C has Pointers. "Philip Lantz" <prl@canterey.us> - 2011-12-17 23:48 -0800
                Re: Why C has Pointers. superpollo <superpollo@tznvy.pbz> - 2011-12-18 11:06 +0100
                Re: Why C has Pointers. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-12-18 13:44 +0000
                Re: Why C has Pointers. pete <pfiland@mindspring.com> - 2011-12-18 11:05 -0500
                Re: Why C has Pointers. James Kuyper <jameskuyper@verizon.net> - 2011-12-18 11:50 -0500
                Re: Why C has Pointers. Keith Thompson <kst-u@mib.org> - 2011-12-18 13:10 -0800
                Re: Why C has Pointers. ralph <nt_consulting64@yahoo.net> - 2011-12-27 16:03 -0600
                Re: Why C has Pointers. Barry Schwarz <schwarzb@dqel.com> - 2011-12-27 14:40 -0800
                Re: Why C has Pointers. Keith Thompson <kst-u@mib.org> - 2011-12-27 15:08 -0800
                Re: Why C has Pointers. ralph <nt_consulting64@yahoo.net> - 2011-12-27 20:09 -0600
                Re: Why C has Pointers. Keith Thompson <kst-u@mib.org> - 2011-12-27 23:00 -0800
                Re: Why C has Pointers. Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-12-28 02:53 -0800
                Re: Why C has Pointers. James Kuyper <jameskuyper@verizon.net> - 2011-12-28 08:39 -0500
                Re: Why C has Pointers. James Kuyper <jameskuyper@verizon.net> - 2011-12-27 18:14 -0500
                Re: Why C has Pointers. Keith Thompson <kst-u@mib.org> - 2011-12-27 15:36 -0800
                Re: Why C has Pointers. James Kuyper <jameskuyper@verizon.net> - 2011-12-27 18:53 -0500
                Re: Why C has Pointers. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-12-28 13:20 +0000
                Re: Why C has Pointers. John Bode <jfbode1029@gmail.com> - 2011-12-28 08:42 -0800
              Re: Why C has Pointers. 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-17 20:36 -0800
                Re: Why C has Pointers. Seebs <usenet-nospam@seebs.net> - 2011-12-18 04:56 +0000
                Re: Why C has Pointers. 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-17 21:37 -0800
                Re: Why C has Pointers. Seebs <usenet-nospam@seebs.net> - 2011-12-18 05:42 +0000
                Re: Why C has Pointers. 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-18 08:08 -0800
                Re: Why C has Pointers. Robert Wessel <robertwessel2@yahoo.com> - 2011-12-18 10:36 -0600
                Re: Why C has Pointers. Seebs <usenet-nospam@seebs.net> - 2011-12-18 18:50 +0000
                Re: Why C has Pointers. Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-12-19 02:39 -0800
                Re: Why C has Pointers. gordonb.1i27q@burditt.org (Gordon Burditt) - 2011-12-21 22:34 -0600
                Re: Why C has Pointers. 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-19 07:56 -0800
                Re: Why C has Pointers. John Tsiombikas <nuclear@member.fsf.org> - 2011-12-19 18:38 +0000
                Re: Why C has Pointers. 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-19 13:47 -0800
                Re: Why C has Pointers. Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-12-28 02:58 -0800
                Re: Why C has Pointers. 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-28 03:21 -0800
            Re: Why C has Pointers. Keith Thompson <kst-u@mib.org> - 2011-12-18 13:07 -0800
              Re: Why C has Pointers. "io_x" <a@b.c.invalid> - 2011-12-19 04:02 +0100
        Re: Why C has Pointers. 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-17 15:35 -0800
          Re: Why C has Pointers. ralph <nt_consulting64@yahoo.net> - 2011-12-17 18:41 -0600
        Re: Why C has Pointers. John Bode <jfbode1029@gmail.com> - 2011-12-19 05:14 -0800
      Re: Why C has Pointers. 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-18 09:50 -0800
      Re: Why C has Pointers. 88888 Dihedral <dihedral88888@googlemail.com> - 2012-01-09 15:48 -0800
        Re: Why C has Pointers. Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-01-10 00:21 -0800
          Re: Why C has Pointers. 88888 Dihedral <dihedral88888@googlemail.com> - 2012-01-10 01:03 -0800
    Re: Why C has Pointers. Keith Thompson <kst-u@mib.org> - 2011-12-17 11:45 -0800
      Re: Why C has Pointers. 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-17 15:18 -0800
      Re: Why C has Pointers. 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-19 14:10 -0800
        Re: Why C has Pointers. 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-20 10:46 -0800
  Re: Why C has Pointers. "Skybuck Flying" <Windows7IsOK@DreamPC2006.com> - 2011-12-17 19:15 +0100
    Re: Why C has Pointers. "Skybuck Flying" <Windows7IsOK@DreamPC2006.com> - 2011-12-17 19:19 +0100
      Re: Why C has Pointers. Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-12-19 02:48 -0800
  Re: Why C has Pointers. 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-18 11:05 -0800
    Re: Why C has Pointers. Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-12-19 02:48 -0800

csiph-web