Re: incrementing a pointer to an array



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?

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

<snip>

.



Relevant Pages

  • Re: A couple of things from H&S
    ... expression of array type decays to a pointer to the array's first ... element *unless* it's in one of three contexts: the operand of sizeof, ... is quite obvious, p is the pointer. ...
    (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: sizeof error
    ... That's because you were applying 'sizeof' to a pointer and not to ... array (or its first element to be precise), ...
    (comp.lang.c)
  • Re: Two dimentional array dereference in C
    ... first element of the array "p", i.e., the entire first row. ... It is only once you follow this pointer value to the entire array, ... is when it is the operand of the sizeof operator. ...
    (comp.lang.c)
  • 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)