473,856 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

incrementing a pointer to an array

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
Aug 30 '08 #1
9 3379
su************* *@yahoo.com, India said:
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.
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
Aug 30 '08 #2
On Fri, 29 Aug 2008 23:29:22 -0700, su************* *@yahoo.com, 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. Incrementing this address is undefined.

for intel compiler, the fist program prints
0 1
0
40896 <<<
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
Aug 30 '08 #3
utab said:
On Fri, 29 Aug 2008 23:29:22 -0700, su************* *@yahoo.com, 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 */
ap = &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?

<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
Aug 30 '08 #4
On Sat, 30 Aug 2008 07:58:05 +0000, Richard Heathfield wrote:
utab said:
>On Fri, 29 Aug 2008 23:29:22 -0700, su************* *@yahoo.com, 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>
Aug 30 '08 #5
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, su************* *@yahoo.com, 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

>>Incrementin g 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 misunderstandin g, 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
Aug 30 '08 #6
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, su************* *@yahoo.com, 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
Incrementi ng 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 misunderstandin g, 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
Aug 30 '08 #7
In article <2e************ *************** *******@z6g2000 pre.googlegroup s.com>,
su************* *@yahoo.com, India <su************ **@yahoo.comwro te:
>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 ?
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.
Aug 30 '08 #8
su************* *@yahoo.com, 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;
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
Aug 30 '08 #9
su************* *@yahoo.com, 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;
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
es*****@ieee-dot-org.invalid
Aug 30 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2897
by: Mothra | last post by:
Here's what I'm trying to do (kill off old Unix logins): --------------------- $i=0; while (<$who>) { chomp($_); my @line = split(/\s+/, $_); # Split it into an array next unless ($line eq "old"); push @{$oldsessions}, @line; $i++;
8
2167
by: BigMan | last post by:
I wonder if the C++ standard says what should happen if I increment a pointer to a one-past-the-end value. I think it says that it is perfectly legal to increment a pointer to the last element element in an array - I get a pointer to one-past-the-end value. It is guaranteed that such a value exists for any array, so I always get a valid pointer to something (I do not care what it is). So, my question is what if I go further and increment...
3
2366
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ---------------------------------------------- //example 1: typedef int t_Array; int main(int argc, char* argv)
204
13164
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 = {0,1,2,4,9};
27
9002
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in advance. Erik
53
4830
by: subramanian100in | last post by:
I saw this question from www.brainbench.com void *ptr; myStruct myArray; ptr = myArray; Which of the following is the correct way to increment the variable "ptr"? Choice 1 ptr = ptr + sizeof(ptr);
7
1931
by: jwhitby3 | last post by:
Hi all, I am trying to develop what amounts to a data entry page for the company I work for, (mostly to make my job easier). I think that I am beginning to grasp php, but I am at a loss now. I understand how to use HTML_Table to add a table to a page, and that portion of my project is coming along nicely. The problem is at this point, that I can't figure out how to increment the row that gets written to. Row 0 just keeps getting...
0
9763
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11060
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10697
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10389
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9532
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectóplanning, coding, testing, and deploymentówithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7932
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Duprť who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7097
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5959
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4175
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.