Re: incrementing a pointer to an array



On Sat, 30 Aug 2008 08:19:07 +0000, Richard Heathfield wrote:

utab said:

On Sat, 30 Aug 2008 07:58:05 +0000, Richard Heathfield wrote:

utab said:

On Fri, 29 Aug 2008 23:29:22 -0700, subramanian100in@xxxxxxxxx, India
wrote:

The following portion is from c-faq.com - comp.lang.c FAQ list ·
Question 6.13

int a1[3] = {0, 1, 2};
int a2[2][3] = {{3, 4, 5}, {6, 7, 8}}; int *ip; /*
pointer to int */
int (*ap)[3]; /* pointer to array [3] of int */\

ap = &a1;
The trick is here because you are getting the address of the pointer
to the first element, If I am not mistaken...

if you use sizeof on ap you will see that it is associated to the
whole array...8 bytes.

Wrong. ap is a pointer to the first element of the array, but using
sizeof on ap is by no means required to result in 8, and indeed on my
system it does not. Nor need it result in 3*sizeof(int) - and again,
on my system it does not.

#include <stdio.h>

int main(void)
{
int a1[3] = {0, 1, 2};
int (*ap)[3]; /* pointer to array [3] of int */

Hi,

ap = &a1;

Could you clarify this statement for me? is not ap pointer to whole
array of 3?

Yes, ap is a pointer to an array of three ints, and a1 is an array of
three ints, so it's perfectly legal to point ap at a1.

printf("**ap = %d\n", **ap);
printf("sizeof ap = %d\n", (int)sizeof ap); ap++; return 0;
}

**ap = 0
sizeof ap = 4


Incrementing this address is undefined.

Why?
not incrementing, dereferencing, sorry

Right - but the OP has made it clear that he already knows dereferencing
such a pointer would be illegal. What he's unclear on is why the FAQ
says that incrementing it (just *one* place) is illegal.

Now that I've looked at the FAQ question in question, it seems to me
that this is simply a misunderstanding, and that the FAQ is at fault,
but only mildly so. What Steve is saying is that if your intent is to
walk through the array's objects, getting a pointer to the array isn't
the way to do it. Rather, you get a pointer to the first element in the
array, and increment /that/, in a loop - which is quite right, of
course. His WRONG applies to the error of treating a pointer-to-array as
if it were a pointer-to-first-element-of-array.

Right, my English ;) and fast read
.



Relevant Pages

  • Re: char **argv & char *argv[]
    ... "pointer to pointer to char". ... >> pointer)) pointing to the first element of an array. ... so we have to start adding more context. ... type "pointer to char", rather than "array MISSING_SIZE of char". ...
    (comp.lang.c)
  • Re: difference between x[10] and (*x)[10]
    ... is converted to pointer to the first element of the array. ... array is used it "decays" to a pointer to the first element. ... conversion to this pointer added in order to maintain ...
    (comp.os.linux.development.apps)
  • Re: declaring a function that returns a pointer to 1-d array
    ... > I also want to know how can we return a pointer to first element ... > a 2d array whose elements are of type int)? ... the first element of arr is an array, not an int. ...
    (comp.lang.c)
  • Re: why cannot assign to function call
    ... hypothetical C-like languages, ... sizeof business would still indicate that a pointer was being passed. ... talk about variables of an array type. ... the earlier version of the standard didn't have numbered ...
    (comp.lang.python)
  • Re: pointer and array help
    ... >> element of the array, which is what i expected the pointer to contain ... >> the hexadecimal value of the first element of the arrray. ... Thanks to the existence of the C-style string, ... and you passed a pointer to the first element to a method ...
    (comp.lang.cpp)