Re: incrementing a pointer to an array
- From: utab <umut.tabak@xxxxxxxxx>
- Date: Sat, 30 Aug 2008 10:29:53 +0200
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 ·pointer to int */
Question 6.13
int a1[3] = {0, 1, 2};
int a2[2][3] = {{3, 4, 5}, {6, 7, 8}}; int *ip; /*
int (*ap)[3]; /* pointer to array [3] of int */\The trick is here because you are getting the address of the pointer
ap = &a1;
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);not incrementing, dereferencing, sorry
printf("sizeof ap = %d\n", (int)sizeof ap); ap++; return 0;
}
**ap = 0
sizeof ap = 4
Incrementing this address is undefined.
Why?
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
.
- References:
- incrementing a pointer to an array
- From: subramanian100in@xxxxxxxxx, India
- Re: incrementing a pointer to an array
- From: utab
- Re: incrementing a pointer to an array
- From: Richard Heathfield
- incrementing a pointer to an array
- Prev by Date: Re: incrementing a pointer to an array
- Next by Date: Re: incrementing a pointer to an array
- Previous by thread: Re: incrementing a pointer to an array
- Next by thread: Re: incrementing a pointer to an array
- Index(es):
Relevant Pages
|