sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
subramanian100in@yahoo.com, India's Avatar

incrementing a pointer to an array


Question posted by: subramanian100in@yahoo.com, India (Guest) on August 30th, 2008 07:35 AM
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;
printf("%d\n", **ap);
ap++; /* WRONG */

Here why is incrementing ap ie 'ap++' mentioned as WRONG ? Isn't it
similar to
int i_int = 100;
int *p = &i_int;
p++;

Here we cannot dereference 'p' but incrementing 'p' only once, is
allowed. Similarly,

ap is assigned '&a1'. Then we increment ap only once. Why is this
WRONG ?

Kindly clarify.

Thanks
V.Subramanian
9 Answers Posted
Richard Heathfield's Avatar
Richard Heathfield August 30th, 2008 07:55 AM
Guest - n/a Posts
#2: Re: incrementing a pointer to an array

Join Bytes!, India said:
Quote:
Originally Posted by
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;
printf("%d\n", **ap);
ap++; /* WRONG */
>
Here why is incrementing ap ie 'ap++' mentioned as WRONG ?


I'm not sure. I don't know of any reason why it's illegal. Maybe Steve just
means it's a lousy idea.
Quote:
Originally Posted by
Isn't it
similar to
int i_int = 100;
int *p = &i_int;
p++;


Yes.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
utab's Avatar
Guest - n/a Posts
#3: Re: incrementing a pointer to an array

On Fri, 29 Aug 2008 23:29:22 -0700, Join Bytes!, India
wrote:
Quote:
Originally Posted by
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 */
Quote:
Originally Posted by
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. Incrementing this address is undefined.

for intel compiler, the fist program prints
0 1
0
40896 <<<
Quote:
Originally Posted by
printf("%d\n", **ap);
ap++; /* WRONG */
>
Here why is incrementing ap ie 'ap++' mentioned as WRONG ? Isn't it
similar to
int i_int = 100;
int *p = &i_int;
p++;
>
Here we cannot dereference 'p' but incrementing 'p' only once, is
allowed. Similarly,
>
ap is assigned '&a1'. Then we increment ap only once. Why is this WRONG
?
>
Kindly clarify.
>
Thanks
V.Subramanian


Richard Heathfield's Avatar
Richard Heathfield August 30th, 2008 08:55 AM
Guest - n/a Posts
#4: Re: incrementing a pointer to an array

utab said:
Quote:
Originally Posted by
On Fri, 29 Aug 2008 23:29:22 -0700, Join Bytes!, India
wrote:
>
Quote:
Originally Posted by
>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 */
Quote:
Originally Posted by
>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 */
ap = &a1;
printf("**ap = %d\n", **ap);
printf("sizeof ap = %d\n", (int)sizeof ap);
ap++;
return 0;
}

**ap = 0
sizeof ap = 4

Quote:
Originally Posted by
Incrementing this address is undefined.


Why?

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
utab's Avatar
Guest - n/a Posts
#5: Re: incrementing a pointer to an array

On Sat, 30 Aug 2008 07:58:05 +0000, Richard Heathfield wrote:
Quote:
Originally Posted by
utab said:
>
Quote:
Originally Posted by
>On Fri, 29 Aug 2008 23:29:22 -0700, Join Bytes!, India
>wrote:
>>
Quote:
Originally Posted by
>>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 */
Quote:
Originally Posted by
>>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?
Quote:
Originally Posted by
printf("**ap = %d\n", **ap);
printf("sizeof ap = %d\n", (int)sizeof ap); ap++;
return 0;
}
>
**ap = 0
sizeof ap = 4
>
>
Quote:
Originally Posted by
>Incrementing this address is undefined.

>
Why?

not incrementing, dereferencing, sorry
Quote:
Originally Posted by
>
<snip>


Richard Heathfield's Avatar
Richard Heathfield August 30th, 2008 09:25 AM
Guest - n/a Posts
#6: Re: incrementing a pointer to an array

utab said:
Quote:
Originally Posted by
On Sat, 30 Aug 2008 07:58:05 +0000, Richard Heathfield wrote:
>
Quote:
Originally Posted by
>utab said:
>>
Quote:
Originally Posted by
>>On Fri, 29 Aug 2008 23:29:22 -0700, Join Bytes!, 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.
Quote:
Originally Posted by
Quote:
Originally Posted by
> printf("**ap = %d\n", **ap);
> printf("sizeof ap = %d\n", (int)sizeof ap); ap++;
> return 0;
>}
>>
>**ap = 0
>sizeof ap = 4
>>
>>
Quote:
Originally Posted by
>>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.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
utab's Avatar
Guest - n/a Posts
#7: Re: incrementing a pointer to an array

On Sat, 30 Aug 2008 08:19:07 +0000, Richard Heathfield wrote:
Quote:
Originally Posted by
utab said:
>
Quote:
Originally Posted by
>On Sat, 30 Aug 2008 07:58:05 +0000, Richard Heathfield wrote:
>>
Quote:
Originally Posted by
>>utab said:
>>>
>>>On Fri, 29 Aug 2008 23:29:22 -0700, Join Bytes!, 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.
>
Quote:
Originally Posted by
Quote:
Originally Posted by
>> 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
Richard Tobin's Avatar
Guest - n/a Posts
#8: Re: incrementing a pointer to an array

In article <2e8c4eff-4156-4352-8279-a75f0e952657@z6g2000pre.googlegroups.com>,
Join Bytes!, India <subramanian100in@yahoo.comwrote:
Quote:
Originally Posted by
>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;
> printf("%d\n", **ap);
> ap++; /* WRONG */

Quote:
Originally Posted by
>Here why is incrementing ap ie 'ap++' mentioned as WRONG ?


Because the next line, which you have omitted, is

printf("%d\n", **ap); /* undefined */

The increment in isolation is not wrong, but if you're intending to
dereference the pointer, it is.

-- Richard
--
Please remember to mention me / in tapes you leave behind.
Martin Ambuhl's Avatar
Guest - n/a Posts
#9: Re: incrementing a pointer to an array

Join Bytes!, India wrote:
Quote:
Originally Posted by
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;
printf("%d\n", **ap);
ap++; /* WRONG */
>
Here why is incrementing ap ie 'ap++' mentioned as WRONG ?


compile and run the following. Your implementation will certainly give
different values and perhaps a different format for the addresses. What
will none the less be true is that when ap = &a, ++ap is not the address
of any element of a. Why? Because ap is not a pointer to int, bur a
pointer to an array[3] of int, and the value of ++ap is the address of
the next array[3] of int.

#include <stdio.h>

int main(void)
{
int a[3] = { 0, 1, 2 };
int (*ap)[3]; /* pointer to array [3] of int */
size_t i, n = sizeof a / sizeof *a;
ap = &a;
printf("sizeof a = %zu, sizeof *a = %zu\n", sizeof a, sizeof *a);
for (i = 0; i < n; i++)
printf("&a[%zu] = %p\n", i, (void *) &a[i]);
putchar('\n');
printf("sizeof ap = %zu, sizeof *ap = %zu\n", sizeof ap,
sizeof *ap);
printf("ap = %p, ", (void *) ap);
printf("++ap = %p\n", (void *) ++ap);
return 0;
}


sizeof a = 12, sizeof *a = 4
&a[0] = dff9c
&a[1] = dffa0
&a[2] = dffa4

sizeof ap = 4, sizeof *ap = 12
ap = dff9c, ++ap = dffa8
Eric Sosman's Avatar
Guest - n/a Posts
#10: Re: incrementing a pointer to an array

Join Bytes!, India wrote:
Quote:
Originally Posted by
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;
printf("%d\n", **ap);
ap++; /* WRONG */
>
Here why is incrementing ap ie 'ap++' mentioned as WRONG ?


It isn't WRONG taken by itself: It increments `ap'
to point one position past the thing it formerly pointed
at, which was the array `a1'. But it is WRONG when used
in combination with the next line in the FAQ's code, which
you snipped but which I now recommend to you for re-study.

--
Eric Sosman
Join Bytes!lid
 
Not the answer you were looking for? Post your question . . .
197,018 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 197,018 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors