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


Groups > comp.lang.c > #24855

Re: 2D arrays, pointers, and pointer arithmetic: question

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: 2D arrays, pointers, and pointer arithmetic: question
Date 2012-08-24 12:01 -0400
Organization A noiseless patient Spider
Message-ID <5037A576.6020506@verizon.net> (permalink)
References <e03d454c-bb78-4a20-be1a-9d082c7e1748@googlegroups.com>

Show all headers | View raw


On 08/24/2012 11:28 AM, gdotone@gmail.com wrote:
> I'm reading "A Book on C", 4th edition. The author gives the following example when discussing two dimensional arrays:
> 
> a[3][5], so let a[][] be of type int.
> 
> 
>            col 1        col 2      col 3       col 4      col 5
> row 1     a[0][0]     a[0][1]     a[0][2]     a[0][3]    a[0][4]
> row 2     a[1][0]     a[1][1]     a[1][2]     a[1][3]    a[1][4]
> row 3     a[2][0]     a[2][1]     a[2][2]     a[2][3]    a[2][4]
> 
> The author then says that the following expression are equivalent
> 
> a[i][j]
> *(a[i] + j)
> (*(a + i))[j]
> *((*(a+i)) + j)
> *(&a[0][0] + 5*i + j)

<pedantic>
This last expression takes a pointer to the first element of an array
containing only 5 elements, and (for i > 0) attempts to dereference it
after adding an integer greater than or equal to 5. Such derefencing has
undefined behavior (6.5.6p8). This interpretation of the standard is
controversial, but IMNSHO correct. In practice, on many machines it will
work exactly as expected. It's unlikely to fail except on systems which
provide run-time array bounds checking, which is rare.

Otherwise, the only way it's likely to fail is if the optimizer takes
advantage of the fact that it has undefined behavior by performing
optimizations that depend upon such code not being executed. For
instance, an implementation that performs an optimization based upon the
assumption that a[0]+i never refers to the same object a[1]+j,
regardless of the values of i and j, is perfectly legal despite the fact
that such an optimization could break code like this.
</pedantic>

> I understand that the table is just a convenient representation of the 2D array.
> In memory the 2D array is actually contiguous. OK. 
> 
> My question has to do with saying or writing out in words what is being shown when using the *, dereferencing, and &, address operators.
> 
> Is this the correct way of saying:
> 
> *(a[i] + j) the base address is pointed to by a[i] and we move along the contiguous memory j (interval), so we add j, j of size int, to get the value at that location we use the dereference operator.
> 
> I believe I understand what's happening, but am I saying it right.

Not quite, a[i] doesn't point at the base address. a[i] is an expression
of array type, and in most contexts (this being one of them) gets
automatically converted to a pointer (address) to the first element of
the array.

> (*(a + i))[j] the base address is at a, i is added to the base address, ok, I understand that we are looking at the row location here, but I'm stuck at how to say this.

a is an expression of array type, which automatically gets converted
into a pointer to the first element of that array, a[0]. Adding 'i' to
that pointer gives the address of the 'i+1'th element of that array,
a[i]. Dereferencing that pointer gives an expression of array type,
referring to a[i]. Like all such expressions, in most contexts, it gets
automatically converted to a pointer to the first element of that array
a[i][0]. Whenever x[j] is a valid expression, it is equivalent to
*(x+j). Adding 'j' to that pointer gives a pointer to the 'j+1'th
element of a[i], which is a[i][j]. Dereferencing that pointer gives the
value of that element.

> let's say addresses are given in decimal, an integer in one byte and each address points to a byte. I hope I'm saying this right?
> at address 100, a[0][0]
> at address 109, a[1][0]
> (if this thought is wrong please clear this up)

a[1][0] is stored at a position that is exactly 5*sizeof a[0][0] bytes
after the position of a[0][0]. Therefore, assuming a linear address
space (which C does NOT require), the difference in the addresses must
be an exact multiple of 5. 109-100=9 doesn't qualify.

> let's also say we are starting at location 100 (decimal).
> consider a[1][0]. row 2, col 1 for the following discussion please.
> 
> (*(a + i ))[j] --- (*(a + 1))[0]
> so, a, the start of the array is pointing to location 100
> What does it mean to add 1 to a?

a + 1 => &a[0] + 1 => &a[1].

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


Thread

2D arrays, pointers, and pointer arithmetic: question gdotone@gmail.com - 2012-08-24 08:28 -0700
  Re: 2D arrays, pointers, and pointer arithmetic: question Adrian Ratnapala <adrian.ratnapala@gmail.com> - 2012-08-24 08:41 -0700
  Re: 2D arrays, pointers, and pointer arithmetic: question James Kuyper <jameskuyper@verizon.net> - 2012-08-24 12:01 -0400
    Re: 2D arrays, pointers, and pointer arithmetic: question 88888 Dihedral <dihedral88888@googlemail.com> - 2012-08-24 11:10 -0700
  Re: 2D arrays, pointers, and pointer arithmetic: question gdotone@gmail.com - 2012-08-27 13:35 -0700
    Re: 2D arrays, pointers, and pointer arithmetic: question James Kuyper <jameskuyper@verizon.net> - 2012-08-27 16:46 -0400
      Re: 2D arrays, pointers, and pointer arithmetic: question Adrian Ratnapala <raka@scooter.fritz.box> - 2012-08-28 19:06 +0000
        Re: 2D arrays, pointers, and pointer arithmetic: question Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-08-28 15:22 -0400
        Re: 2D arrays, pointers, and pointer arithmetic: question James Kuyper <jameskuyper@verizon.net> - 2012-08-28 15:34 -0400
          Re: 2D arrays, pointers, and pointer arithmetic: question Keith Thompson <kst-u@mib.org> - 2012-08-28 14:11 -0700
            Re: 2D arrays, pointers, and pointer arithmetic: question James Kuyper <jameskuyper@verizon.net> - 2012-08-28 17:26 -0400
            Re: 2D arrays, pointers, and pointer arithmetic: question "io_x" <a@b.c.invalid> - 2012-08-29 09:25 +0200
          Re: 2D arrays, pointers, and pointer arithmetic: question Adrian Ratnapala <raka@scooter.fritz.box> - 2012-08-29 07:20 +0000
            Re: 2D arrays, pointers, and pointer arithmetic: question James Kuyper <jameskuyper@verizon.net> - 2012-08-29 07:17 -0400
              Re: 2D arrays, pointers, and pointer arithmetic: question Adrian Ratnapala <raka@scooter.fritz.box> - 2012-08-29 11:41 +0000
            Re: 2D arrays, pointers, and pointer arithmetic: question Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-08-29 08:21 -0400
              Re: 2D arrays, pointers, and pointer arithmetic: question Keith Thompson <kst-u@mib.org> - 2012-08-29 11:37 -0700
                Re: 2D arrays, pointers, and pointer arithmetic: question Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> - 2012-08-29 20:05 +0000
                Re: 2D arrays, pointers, and pointer arithmetic: question James Kuyper <jameskuyper@verizon.net> - 2012-08-29 17:27 -0400
                Re: 2D arrays, pointers, and pointer arithmetic: question Keith Thompson <kst-u@mib.org> - 2012-08-29 18:05 -0700
                Re: 2D arrays, pointers, and pointer arithmetic: question Les Cargill <lcargill99@comcast.com> - 2012-08-29 20:14 -0500
                Re: 2D arrays, pointers, and pointer arithmetic: question Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-08-30 04:47 -0700
                Re: 2D arrays, pointers, and pointer arithmetic: question Richard Damon <news.x.richarddamon@xoxy.net> - 2012-09-01 23:47 -0400
                Re: 2D arrays, pointers, and pointer arithmetic: question 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-02 04:43 -0700
                Re: 2D arrays, pointers, and pointer arithmetic: question Richard Damon <news.x.richarddamon@xoxy.net> - 2012-09-02 23:12 -0400
                Re: 2D arrays, pointers, and pointer arithmetic: question ais523 <ais523@nethack4.org> - 2012-08-30 11:35 +0000
                Re: 2D arrays, pointers, and pointer arithmetic: question ralph <nt_consulting64@yahoo.com> - 2012-08-30 11:17 -0500
                Re: 2D arrays, pointers, and pointer arithmetic: question Keith Thompson <kst-u@mib.org> - 2012-08-30 13:52 -0700
                Re: 2D arrays, pointers, and pointer arithmetic: question Anders Wegge Keller <wegge@wegge.dk> - 2012-08-30 23:13 +0200
                Re: 2D arrays, pointers, and pointer arithmetic: question ralph <nt_consulting64@yahoo.com> - 2012-08-31 16:37 -0500
    Re: 2D arrays, pointers, and pointer arithmetic: question Barry Schwarz <schwarzb@dqel.com> - 2012-08-27 19:45 -0700
      Re: 2D arrays, pointers, and pointer arithmetic: question Keith Thompson <kst-u@mib.org> - 2012-08-27 20:40 -0700
        Re: 2D arrays, pointers, and pointer arithmetic: question Tim Rentsch <txr@alumni.caltech.edu> - 2012-09-07 06:28 -0700

csiph-web