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


Groups > comp.lang.c++ > #3440

On generally accepted terminology for pointers and arrays

From "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com>
Newsgroups comp.lang.c++
Subject On generally accepted terminology for pointers and arrays
Date 2011-04-12 04:00 +0200
Organization A noiseless patient Spider
Message-ID <io0bnr$dph$1@dont-email.me> (permalink)

Show all headers | View raw


Since my killfile works I only see one side of the numerous debates here about 
pointers and arrays.

But it does seem to be pretty confrontational.

So, herewith, me on the subject of how to talk about pointers & arrays.

When I wrote my (peer reviewed) pointers tutorial I discovered that even the 
acknowledged international experts did not agree on the meaning of "pointer". 
Happily the disagreements were mainly of the single kind exemplified by

    Hey, you can't call THAT a pointer (because I don't)!

Some objected to calling variables pointers, others objected to calling pointer 
values pointers, and yet others objected to calling pointer types pointers, even 
though the Holy Standard does. In case the latter is difficult to grok: I'm 
talking about utterances like "it's a pointer" and "it's a car", instead of 
spelling it out more literally like "it's a something of pointer type" and "it's 
an object that conforms to the requirements of the 'car' type of vehicle". I 
never understood the point of making such subtle distinctions, but apparently 
some who are smarter and/or more knowledgable than me, do. And what about smart 
pointers? If they are accepted as pointers, then a pointer needs not necessarily 
be a pointer of the kind that the language supports directly. So what we have is 
a word that becomes more vague the larger the context is.

Happily, that means that as one narrows the context, one also narrows down the 
possible meanings of "pointer". Narrowing it down to the pointers directly 
supported by the C++ language, the RAW POINTERS of C++, there is almost no 
ambiguity. In this very limited context the term is well-defined.

Now, three questions that I have seen mentioned often in the debates, are

   1) Is an array a pointer?

   2) Can a pointer point to an array?

   3) Can a pointer of type T* be said to point to an array of T?

Regarding question (1), is an array a pointer?, the answer is NO.

Some newbies think the answer is yes because lots of things are very much the 
same for pointers and arrays. But the crucial thing is not how operationally 
similar they are. Instead, the crucial thing is how they differ, for example 
that an array can occupy an arbitrary amount of storage, while the size of a 
pointer  --  e.g. as reported by the sizeof operator  --  is pretty small.

Regarding question (2), can a pointer point to an array?

Here the answer is YES, and, /in two different ways/.

First, a pointer can point to an array in the Pascal sense, where the pointer is 
of type "pointer to array" and points to some array (or else is null), like

   int a[42];
   int (*p1)[42] = &a;  // OK, p1 is a pointer to array (type-wise).

It would be wrong to omit the "42" in the pointer declaration, because p1 is 
very very strictly a pointer to an array *of a given size*. You would not be 
able to e.g. "++" increment the pointer if the pointee type was of some unknown 
size. And although you can have pointers to incomplete types (types of unknown 
size (uh, I mean, types whose instances are of unknown size (or, more 
pedantically, types whose instances, if they could exist, would be of unknown 
size (hm, ok, I direct you to the Holy Standard for absolute precision)))), in 
this specific case the language does not permit it  --  I don't know why.

So, in the case above we have a pointer variable that points to an array, *and* 
that is of type pointer to array.

The second way in which a pointer can point to an array, is the answer to 
question 3, namely

   int a[42];
   int* p2 = a;   // OK, p2 is a pointer to an array (dynamically).

In this case, however, the type of the pointer is not a pointer to array, but 
pointer to ElementType. It's only the dynamic situation that right after the 
above declaration, p2 points to array a. Still, this is the most common 
situation (the p1 thing is extremely rare!), and it would be silly to talk about 
"points, at this point in the program execution, to the first element of an 
array", sort of like a lawyer, instead of just saying "points to an array".

So, the answer to question (3) is also YES.

Summing up:

   1) A pointer is not an array. An array is not a pointer. They can be
      differentiated in many ways (e.g. sizeof, typeid, template tricks,
      not to mention formal arguments of reference type).

   2) A pointer can, however, point to an array.

   3) And a pointer of type T* be said to point to an array of T, but only
      in the dynamic sense, like "right now it points to an array".

I've seen that some people think (3) is just metaphorical. That to be 
technically correct one would need to insert lawyeresque verbiage like "first 
element of". That seems silly to me, and except for the trolling-threads here 
I've never heard anyone speaking that way, or read anyone writing that way.


Cheers & hth.,

- Alf

-- 
blog at <url: http://alfps.wordpress.com>

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


Thread

On generally accepted terminology for pointers and arrays "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-04-12 04:00 +0200
  Re: On generally accepted terminology for pointers and arrays Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2011-04-12 00:13 -0400
  Re: On generally accepted terminology for pointers and arrays "Paul" <pchristor@yahoo.co.uk> - 2011-04-12 05:17 +0100
    Re: On generally accepted terminology for pointers and arrays Leigh Johnston <leigh@i42.co.uk> - 2011-04-12 14:02 +0100
      Re: On generally accepted terminology for pointers and arrays "Paul" <pchristor@yahoo.co.uk> - 2011-04-12 14:10 +0100
        Re: On generally accepted terminology for pointers and arrays Leigh Johnston <leigh@i42.co.uk> - 2011-04-12 14:49 +0100
      Re: On generally accepted terminology for pointers and arrays cg_chas <cg_chas@hotmail.com> - 2011-04-12 10:05 -0400
  Re: On generally accepted terminology for pointers and arrays cg_chas <cg_chas@hotmail.com> - 2011-04-12 01:11 -0400
  Re: On generally accepted terminology for pointers and arrays gwowen <gwowen@gmail.com> - 2011-04-11 23:24 -0700
    Re: On generally accepted terminology for pointers and arrays gwowen <gwowen@gmail.com> - 2011-04-12 00:16 -0700
      Re: On generally accepted terminology for pointers and arrays Leigh Johnston <leigh@i42.co.uk> - 2011-04-12 13:53 +0100
        Re: On generally accepted terminology for pointers and arrays "Paul" <pchristor@yahoo.co.uk> - 2011-04-12 14:08 +0100
      Re: On generally accepted terminology for pointers and arrays "Paul" <pchristor@yahoo.co.uk> - 2011-04-12 14:05 +0100
        Re: On generally accepted terminology for pointers and arrays Leigh Johnston <leigh@i42.co.uk> - 2011-04-12 14:48 +0100
          Re: On generally accepted terminology for pointers and arrays Ian Collins <ian-news@hotmail.com> - 2011-04-13 07:53 +1200
      Re: On generally accepted terminology for pointers and arrays "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-04-12 22:14 +0200
        Re: On generally accepted terminology for pointers and arrays Öö Tiib <ootiib@hot.ee> - 2011-04-12 15:39 -0700
    Re: On generally accepted terminology for pointers and arrays Goran <goran.pusic@gmail.com> - 2011-04-12 01:39 -0700
      Re: On generally accepted terminology for pointers and arrays Ian Collins <ian-news@hotmail.com> - 2011-04-12 20:51 +1200
    Re: On generally accepted terminology for pointers and arrays Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-04-12 21:51 +0000
  Re: On generally accepted terminology for pointers and arrays cpp4ever <n2xssvv.g02gfr12930@ntlworld.com> - 2011-04-12 09:59 +0100
    Re: On generally accepted terminology for pointers and arrays "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-04-12 16:25 +0200
      Re: On generally accepted terminology for pointers and arrays cpp4ever <n2xssvv.g02gfr12930@ntlworld.com> - 2011-04-12 16:07 +0100
        Re: On generally accepted terminology for pointers and arrays "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-04-12 17:32 +0200
          Re: On generally accepted terminology for pointers and arrays cpp4ever <n2xssvv.g02gfr12930@ntlworld.com> - 2011-04-12 16:53 +0100
            Re: On generally accepted terminology for pointers and arrays "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-04-12 18:16 +0200
              Re: On generally accepted terminology for pointers and arrays cpp4ever <n2xssvv.g02gfr12930@ntlworld.com> - 2011-04-12 17:39 +0100
                Re: On generally accepted terminology for pointers and arrays "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-04-12 19:32 +0200
                Re: DILLIGAF cpp4ever <n2xssvv.g02gfr12930@ntlworld.com> - 2011-04-12 19:05 +0100
                Re: DILLIGAF Peter Remmers <p.remmers@expires-2011-04-30.arcornews.de> - 2011-04-12 20:36 +0200
                Re: DILLIGAF cpp4ever <n2xssvv.g02gfr12930@ntlworld.com> - 2011-04-12 20:47 +0100
                Re: DILLIGAF gwowen <gwowen@gmail.com> - 2011-04-12 14:48 -0700
                Re: DILLIGAF Öö Tiib <ootiib@hot.ee> - 2011-04-12 15:16 -0700
    Re: On generally accepted terminology for pointers and arrays "Paul" <pchristor@yahoo.co.uk> - 2011-04-12 17:48 +0100
  Re: On generally accepted terminology for pointers and arrays Noah Roberts <dont@email.me> - 2011-04-12 09:37 -0700
    Re: On generally accepted terminology for pointers and arrays "Paul" <pchristor@yahoo.co.uk> - 2011-04-12 18:34 +0100
    Re: On generally accepted terminology for pointers and arrays "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-04-12 19:38 +0200
      Re: On generally accepted terminology for pointers and arrays Noah Roberts <dont@email.me> - 2011-04-12 11:00 -0700
        Re: On generally accepted terminology for pointers and arrays "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-04-12 20:03 +0200
          Re: On generally accepted terminology for pointers and arrays Noah Roberts <dont@email.me> - 2011-04-12 11:06 -0700
            Re: On generally accepted terminology for pointers and arrays Peter Remmers <p.remmers@expires-2011-04-30.arcornews.de> - 2011-04-12 20:26 +0200
              Re: On generally accepted terminology for pointers and arrays Noah Roberts <dont@email.me> - 2011-04-12 11:33 -0700
                Re: On generally accepted terminology for pointers and arrays Peter Remmers <p.remmers@expires-2011-04-30.arcornews.de> - 2011-04-12 20:55 +0200
                Re: On generally accepted terminology for pointers and arrays "Paul" <pchristor@yahoo.co.uk> - 2011-04-12 20:14 +0100
                Re: On generally accepted terminology for pointers and arrays Noah Roberts <dont@email.me> - 2011-04-12 12:47 -0700
                Re: On generally accepted terminology for pointers and arrays Peter Remmers <p.remmers@expires-2011-04-30.arcornews.de> - 2011-04-12 22:40 +0200
      Re: On generally accepted terminology for pointers and arrays cg_chas <cg_chas@hotmail.com> - 2011-04-12 14:15 -0400
        Re: On generally accepted terminology for pointers and arrays "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-04-12 20:52 +0200
          Re: On generally accepted terminology for pointers and arrays "Paul" <pchristor@yahoo.co.uk> - 2011-04-12 20:23 +0100
          Re: On generally accepted terminology for pointers and arrays cg_chas <cg_chas@hotmail.com> - 2011-04-12 16:01 -0400
            Re: On generally accepted terminology for pointers and arrays Noah Roberts <dont@email.me> - 2011-04-12 13:46 -0700
              Re: On generally accepted terminology for pointers and arrays Öö Tiib <ootiib@hot.ee> - 2011-04-12 19:49 -0700
                Re: On generally accepted terminology for pointers and arrays hanukas <jukka@liimatta.org> - 2011-04-13 00:06 -0700
                Re: On generally accepted terminology for pointers and arrays cg_chas <cg_chas@hotmail.com> - 2011-04-13 04:14 -0400
                Re: On generally accepted terminology for pointers and arrays Öö Tiib <ootiib@hot.ee> - 2011-04-13 07:53 -0700
                Re: On generally accepted terminology for pointers and arrays "Paul" <pchristor@yahoo.co.uk> - 2011-04-13 09:52 +0100
                Re: On generally accepted terminology for pointers and arrays cg_chas <cg_chas@hotmail.com> - 2011-04-13 05:20 -0400
                Re: On generally accepted terminology for pointers and arrays "Paul" <pchristor@yahoo.co.uk> - 2011-04-13 10:31 +0100
                Re: On generally accepted terminology for pointers and arrays cg_chas <cg_chas@hotmail.com> - 2011-04-13 07:04 -0400
                Re: On generally accepted terminology for pointers and arrays "Paul" <pchristor@yahoo.co.uk> - 2011-04-13 12:35 +0100
                Re: On generally accepted terminology for pointers and arrays cg_chas <cg_chas@hotmail.com> - 2011-04-13 07:41 -0400
                Re: On generally accepted terminology for pointers and arrays Ian Collins <ian-news@hotmail.com> - 2011-04-14 07:47 +1200
                Re: On generally accepted terminology for pointers and arrays "Paul" <pchristor@yahoo.co.uk> - 2011-04-14 11:44 +0100
                Re: On generally accepted terminology for pointers and arrays "io_x" <a@b.c.invalid> - 2011-04-13 18:41 +0200
                Re: On generally accepted terminology for pointers and arrays Noah Roberts <dont@email.me> - 2011-04-13 09:35 -0700
                Re: On generally accepted terminology for pointers and arrays "Paul" <pchristor@yahoo.co.uk> - 2011-04-13 20:17 +0100
              Re: On generally accepted terminology for pointers and arrays cg_chas <cg_chas@hotmail.com> - 2011-04-13 04:33 -0400
                Re: On generally accepted terminology for pointers and arrays cg_chas <cg_chas@hotmail.com> - 2011-04-13 04:35 -0400
            Re: On generally accepted terminology for pointers and arrays "Paul" <pchristor@yahoo.co.uk> - 2011-04-13 01:19 +0100
              Re: On generally accepted terminology for pointers and arrays cg_chas <cg_chas@hotmail.com> - 2011-04-13 04:01 -0400
                Re: On generally accepted terminology for pointers and arrays cg_chas <cg_chas@hotmail.com> - 2011-04-13 04:20 -0400
                Re: On generally accepted terminology for pointers and arrays "Paul" <pchristor@yahoo.co.uk> - 2011-04-13 09:56 +0100
                Re: On generally accepted terminology for pointers and arrays cg_chas <cg_chas@hotmail.com> - 2011-04-13 05:24 -0400
                Re: On generally accepted terminology for pointers and arrays "Paul" <pchristor@yahoo.co.uk> - 2011-04-13 10:34 +0100
                Re: On generally accepted terminology for pointers and arrays cg_chas <cg_chas@hotmail.com> - 2011-04-13 07:07 -0400
                Re: On generally accepted terminology for pointers and arrays "Paul" <pchristor@yahoo.co.uk> - 2011-04-13 12:36 +0100
                Re: On generally accepted terminology for pointers and arrays cg_chas <cg_chas@hotmail.com> - 2011-04-13 07:43 -0400
                Re: On generally accepted terminology for pointers and arrays "Paul" <pchristor@yahoo.co.uk> - 2011-04-13 13:18 +0100
                Re: On generally accepted terminology for pointers and arrays cg_chas <cg_chas@hotmail.com> - 2011-04-13 08:30 -0400
            Re: On generally accepted terminology for pointers and arrays "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-04-13 15:57 +0200
              Re: On generally accepted terminology for pointers and arrays Leigh Johnston <leigh@i42.co.uk> - 2011-04-13 15:10 +0100
              Re: On generally accepted terminology for pointers and arrays cg_chas <cg_chas@hotmail.com> - 2011-04-13 11:09 -0400
                Re: On generally accepted terminology for pointers and arrays cg_chas <cg_chas@hotmail.com> - 2011-04-13 12:04 -0400
            Re: On generally accepted terminology for pointers and arrays Noah Roberts <dont@email.me> - 2011-04-13 09:56 -0700
              Re: On generally accepted terminology for pointers and arrays cg_chas <cg_chas@hotmail.com> - 2011-04-13 14:11 -0400
      Re: On generally accepted terminology for pointers and arrays "Paul" <pchristor@yahoo.co.uk> - 2011-04-12 19:29 +0100
  Re: On generally accepted terminology for pointers and arrays Joshua Maurice <joshuamaurice@gmail.com> - 2011-04-12 09:36 -0700

csiph-web