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


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

Re: Pointer Problem

From jt@toerring.de (Jens Thoms Toerring)
Newsgroups comp.lang.c++
Subject Re: Pointer Problem
Date 2012-02-18 22:39 +0000
Organization Freie Universitaet Berlin
Message-ID <9qank7FfvjU1@mid.uni-berlin.de> (permalink)
References <3e24138a-c351-42fa-ab7f-bb7c3117a232@og8g2000pbb.googlegroups.com> <4f3d1d82$0$4381$7b1e8fa0@news.nbl.fi> <9q5v0qFgutU1@mid.uni-berlin.de> <4f3e0069$0$3078$7b1e8fa0@news.nbl.fi>

Show all headers | View raw


Juha Nieminen <nospam@thanks.invalid> wrote:
> Jens Thoms Toerring <jt@toerring.de> wrote:
> > Juha Nieminen <nospam@thanks.invalid> wrote:
> >> bintom <binoythomas1108@gmail.com> wrote:
> >> > What is the difference between the following declarations?
> >> > 
> >> > int *ptr[10];
> >> > 
> >> >    and
> >> > 
> >> > int (*ptr)[10];
> > 
> >>   The former is an array of 10 elements, each element being an int*.
> > 
> >>   The latter is a pointer to a two-dimensional array (iow. a single
> >> array which is indexed using two index values), the second dimension
> >> of said array being 10.
> > 
> > Sorry for chiming in, but the second is just a pointer to
> > an array of 10 ints, nothing two-dimensional involved at
> > this stage

>   If you want to be really pedantic, that might be so. It's the same as
> with an "int*" which doubles as a pointer to a single int and a pointer
> to an array of ints.

Ok, let me really pedantic;-) An "int*" doesnt double as a pointer
to an array (of ints), it just can point to an element of an array
(just as it can point to a "lonely" int).

> In practice "int*" is basically always a pointer to an array of ints

There are a lot of cases where there are pointers to single ints,
e.g. when passing them to a function (admitedly, more often in C
were ypu don't have references and pointers are the only way to
pass something to a function that can be modified from within
the function).

> because it has intrinsic support for it being an array (ie. you can
> index the potential array behind it). If it's pointing to a single int
> you can think of it as pointing to an array of size 1.

I understand what you mean, but this is saddling the horse from
the back. An "int*" never points to more than a single int. Due
to the shortcoming of C (and thus C++) that arrays aren't "first
class" objects (i.e. they can't be passed to functions by value
since an array isn't a "value", contrary to e.g. structures) a
special case was introduced: when an array appears in a place
were a value is required then the array gets converted to a
pointer to the first element of that array. And this has
resulted in many people (even experienced C and C++ program-
mers) getting confused about the distinction between arrays
and pointers. But that doesn't make a pointer anything "array-
like" - it's just that in certain situations when an array is
found it is converted silently to a pointer to that array's
first element. That's all there's about "intrinsic support".

> Likewise "int (*ptr)[10]" is always a pointer to a two-dimensional array

Sorry, but no. Like an "int*" only points to a single int, a "int
(*ptr)[10]" points to just a single array of 10 ints. But because,
when incremented, it points to the next following array of 10
ints it can be used to iterate over the sub-arrays of a two-
dimensional array when that is an array of arrays of 10 ints.

I can just recommend reading what Chris Torek writes about all
this (even though it doesn't mention C++, but it's exactly the
same in C++) - it's the most lucid explanation I've ever found.

                           Best regards, Jens
-- 
  \   Jens Thoms Toerring  ___      jt@toerring.de
   \__________________________      http://toerring.de

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


Thread

Pointer Problem bintom <binoythomas1108@gmail.com> - 2012-02-16 06:51 -0800
  Re: Pointer Problem Juha Nieminen <nospam@thanks.invalid> - 2012-02-16 15:15 +0000
    Re: Pointer Problem 88888 Dihedral <dihedral88888@googlemail.com> - 2012-02-16 07:17 -0800
      Re: Pointer Problem Victor Bazarov <v.bazarov@comcast.invalid> - 2012-02-16 11:15 -0500
        Re: Pointer Problem scott@slp53.sl.home (Scott Lurndal) - 2012-02-16 17:18 +0000
          Re: Pointer Problem Victor Bazarov <v.bazarov@comcast.invalid> - 2012-02-16 13:46 -0500
            Re: Pointer Problem Mike McCarty <mcmccarty@gmail.com> - 2012-02-16 12:41 -0800
            Re: Pointer Problem scott@slp53.sl.home (Scott Lurndal) - 2012-02-16 20:45 +0000
            Re: Pointer Problem Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-02-17 01:44 -0800
    Re: Pointer Problem jt@toerring.de (Jens Thoms Toerring) - 2012-02-17 03:14 +0000
      Re: Pointer Problem Juha Nieminen <nospam@thanks.invalid> - 2012-02-17 07:23 +0000
        Re: Pointer Problem jt@toerring.de (Jens Thoms Toerring) - 2012-02-18 22:39 +0000
          Re: Pointer Problem Peter Remmers <p.remmers@expires-2011-04-30.arcornews.de> - 2012-02-20 21:07 +0100
            Re: Pointer Problem Paul <pchristor@yahoo.co.uk> - 2012-02-21 18:49 -0800
          Re: Pointer Problem Paul <pchristor@yahoo.co.uk> - 2012-02-21 18:49 -0800
            Re: Pointer Problem Juha Nieminen <nospam@thanks.invalid> - 2012-02-22 10:15 +0000
              Re: Pointer Problem Paul <pchristor@yahoo.co.uk> - 2012-02-22 07:49 -0800
                Re: Pointer Problem Leigh Johnston <leigh@i42.co.uk> - 2012-02-22 20:24 +0000
                Re: Pointer Problem Juha Nieminen <nospam@thanks.invalid> - 2012-02-23 10:36 +0000
                Re: Pointer Problem "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2012-02-23 18:59 +0000
                Re: Pointer Problem Leigh Johnston <leigh@i42.co.uk> - 2012-02-23 19:15 +0000
                Re: Pointer Problem "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2012-02-23 21:08 +0000
                Re: Pointer Problem Leigh Johnston <leigh@i42.co.uk> - 2012-02-23 21:17 +0000
                Re: Pointer Problem Leigh Johnston <leigh@i42.co.uk> - 2012-02-23 21:29 +0000
                Re: Pointer Problem Joshua Maurice <joshuamaurice@gmail.com> - 2012-02-23 17:06 -0800
                Re: Pointer Problem Paul <pchristor@yahoo.co.uk> - 2012-02-24 03:42 -0800
                Re: Pointer Problem Leigh Johnston <leigh@i42.co.uk> - 2012-02-24 12:33 +0000
                Re: Pointer Problem "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2012-02-24 15:09 +0000
                Re: Pointer Problem Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-02-23 20:53 +0000
                Re: Pointer Problem Paul <pchristor@yahoo.co.uk> - 2012-02-24 04:11 -0800
                Re: Pointer Problem Leigh Johnston <leigh@i42.co.uk> - 2012-02-24 12:36 +0000
                Re: Pointer Problem Paul <pchristor@yahoo.co.uk> - 2012-02-24 06:07 -0800
                Re: Pointer Problem Leigh Johnston <leigh@i42.co.uk> - 2012-02-24 14:11 +0000
                Re: Pointer Problem "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2012-02-24 15:49 +0000
                Re: Pointer Problem Mike McCarty <mcmccarty@gmail.com> - 2012-02-24 08:50 -0800
                Re: Pointer Problem "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2012-02-24 18:47 +0000
                Re: Pointer Problem Paul <pchristor@yahoo.co.uk> - 2012-02-24 13:44 -0800
                Re: Pointer Problem Paul <pchristor@yahoo.co.uk> - 2012-02-26 12:41 -0800
                Re: Pointer Problem Mike McCarty <mcmccarty@gmail.com> - 2012-02-26 21:30 -0800
                Re: Pointer Problem Paul <pchristor@yahoo.co.uk> - 2012-02-27 06:07 -0800
                Re: Pointer Problem Mike McCarty <mcmccarty@gmail.com> - 2012-02-27 07:25 -0800
                Re: Pointer Problem Paul <pchristor@yahoo.co.uk> - 2012-02-27 08:17 -0800
                Re: Pointer Problem Mike McCarty <mcmccarty@gmail.com> - 2012-02-27 08:51 -0800
                Re: Pointer Problem Ian Collins <ian-news@hotmail.com> - 2012-02-28 07:18 +1300
                Re: Pointer Problem Paul <pchristor@yahoo.co.uk> - 2012-02-27 11:32 -0800
                Re: Pointer Problem Paul <pchristor@yahoo.co.uk> - 2012-02-27 11:32 -0800
                Re: Pointer Problem Leigh Johnston <leigh@i42.co.uk> - 2012-02-27 19:17 +0000
                Re: Pointer Problem Paul <pchristor@yahoo.co.uk> - 2012-02-27 11:49 -0800
                Re: Pointer Problem Leigh Johnston <leigh@i42.co.uk> - 2012-02-27 20:04 +0000
                Re: Pointer Problem Paul <pchristor@yahoo.co.uk> - 2012-02-27 12:55 -0800
                Re: Pointer Problem Leigh Johnston <leigh@i42.co.uk> - 2012-02-27 21:15 +0000
                Re: Pointer Problem Paul <pchristor@yahoo.co.uk> - 2012-02-27 14:26 -0800
                Re: Pointer Problem Paul <pchristor@yahoo.co.uk> - 2012-02-27 11:21 -0800
  Re: Pointer Problem SG <s.gesemann@gmail.com> - 2012-02-16 07:00 -0800
  Re: Pointer Problem LR <lruss@superlink.net> - 2012-02-16 16:49 -0500
    Re: Pointer Problem Juha Nieminen <nospam@thanks.invalid> - 2012-02-16 22:18 +0000
      Re: Pointer Problem LR <lruss@superlink.net> - 2012-02-16 23:40 -0500
        Re: Pointer Problem Juha Nieminen <nospam@thanks.invalid> - 2012-02-17 07:24 +0000
          Re: Pointer Problem LR <lruss@superlink.net> - 2012-02-17 09:48 -0500
            Re: Pointer Problem Juha Nieminen <nospam@thanks.invalid> - 2012-02-17 15:13 +0000

csiph-web