Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Is pointer arithmetic associative?

Reply
Thread Tools

Is pointer arithmetic associative?

 
 
Francois Grieu
Guest
Posts: n/a
 
      11-08-2005
Are these programs correct ?

#include <stdio.h>
unsigned char a[2] = {1,2};
int main(void) {
unsigned char j;
for(j=1; j<=2; ++j)
printf("%u\n", *( a+j-1 ));
return 0; }


#include <stdio.h>
unsigned char a[2] = {1,2};
int main(void) {
unsigned char j;
for(j=1; j<=2; ++j)
printf("%u\n", *( a-1+j ));
return 0; }


#include <stdio.h>
unsigned char a[2] = {1,2};
int main(void) {
unsigned char j;
for(j=1; j<=2; ++j)
printf("%u\n", *( a+(j-1) ));
return 0; }

rot-13 spoiler: bayl gur svefg bar vf pbeerpg.


One compiler I use generates correct and tight code for the
second form; but it fails for *(-1+a+j) [not due to an
architectural restriction, but due to a plain compiler bug].

Isn't it a shame C programming is so complex ? Given that on
most platforms, pointer arithmetic is associative, and so many
programs out there assume it blindly, is not it time for a
future C standard to waive these restrictions on pointer
associativity in at least a branch of the standard, say with
a predefined constant __ASSOCIATIVE_POINTERS__ ?


Francois Grieu
 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      11-08-2005
Francois Grieu <(E-Mail Removed)> wrote:

> Are these programs correct ?
>
> #include <stdio.h>
> unsigned char a[2] = {1,2};
> int main(void) {
> unsigned char j;
> for(j=1; j<=2; ++j)
> printf("%u\n", *( a+j-1 ));
> return 0; }
>
> #include <stdio.h>
> unsigned char a[2] = {1,2};
> int main(void) {
> unsigned char j;
> for(j=1; j<=2; ++j)
> printf("%u\n", *( a-1+j ));
> return 0; }
>
>
> #include <stdio.h>
> unsigned char a[2] = {1,2};
> int main(void) {
> unsigned char j;
> for(j=1; j<=2; ++j)
> printf("%u\n", *( a+(j-1) ));
> return 0; }
>
> rot-13 spoiler: bayl gur svefg bar vf pbeerpg.


What's wrong with the third? The error in the second is clear; but the
third generates a+(1-1) and a+(2-1), that is, a+0 and a+1, neither of
which is a problem.

Richard
 
Reply With Quote
 
 
 
 
Francois Grieu
Guest
Posts: n/a
 
      11-09-2005
In article <(E-Mail Removed)4all.nl>,
http://www.velocityreviews.com/forums/(E-Mail Removed) (Richard Bos) wrote:

> Francois Grieu <(E-Mail Removed)> wrote:
>
> > Are these programs correct ?
> >
> > #include <stdio.h>
> > unsigned char a[2] = {1,2};
> > int main(void) {
> > unsigned char j;
> > for(j=1; j<=2; ++j)
> > printf("%u\n", *( a+j-1 ));
> > return 0; }
> >
> > #include <stdio.h>
> > unsigned char a[2] = {1,2};
> > int main(void) {
> > unsigned char j;
> > for(j=1; j<=2; ++j)
> > printf("%u\n", *( a-1+j ));
> > return 0; }
> >
> >
> > #include <stdio.h>
> > unsigned char a[2] = {1,2};
> > int main(void) {
> > unsigned char j;
> > for(j=1; j<=2; ++j)
> > printf("%u\n", *( a+(j-1) ));
> > return 0; }
> >
> > rot-13 spoiler: bayl gur svefg bar vf pbeerpg.

>
> What's wrong with the third?


Nothing. The spoiler was wrong..

> The error in the second is clear;


Yes. The first is wrong too, because a+2 is undefined.

Francois Grieu
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      11-09-2005
Francois Grieu <(E-Mail Removed)> wrote:

> In article <(E-Mail Removed)4all.nl>,
> (E-Mail Removed) (Richard Bos) wrote:
>
> > Francois Grieu <(E-Mail Removed)> wrote:
> >
> > > Are these programs correct ?
> > >
> > > #include <stdio.h>
> > > unsigned char a[2] = {1,2};
> > > int main(void) {
> > > unsigned char j;
> > > for(j=1; j<=2; ++j)
> > > printf("%u\n", *( a+j-1 ));
> > > return 0; }
> > >
> > > #include <stdio.h>
> > > unsigned char a[2] = {1,2};
> > > int main(void) {
> > > unsigned char j;
> > > for(j=1; j<=2; ++j)
> > > printf("%u\n", *( a-1+j ));
> > > return 0; }
> > >
> > >
> > > #include <stdio.h>
> > > unsigned char a[2] = {1,2};
> > > int main(void) {
> > > unsigned char j;
> > > for(j=1; j<=2; ++j)
> > > printf("%u\n", *( a+(j-1) ));
> > > return 0; }
> > >
> > > rot-13 spoiler: bayl gur svefg bar vf pbeerpg.

> >
> > What's wrong with the third?

>
> Nothing. The spoiler was wrong..
>
> > The error in the second is clear;

>
> Yes. The first is wrong too, because a+2 is undefined.


No. The first is correct. You are allowed to _calculate_ the address one
past the end of an array. What you can't do is a. dereference said
one-past array or b. calculate any addresses beyond that.

This can be useful for the termination condition of loops over arrays.

Richard
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      11-09-2005
Richard Bos wrote:
>
> Francois Grieu <(E-Mail Removed)> wrote:
>
> > In article <(E-Mail Removed)4all.nl>,
> > (E-Mail Removed) (Richard Bos) wrote:
> >
> > > Francois Grieu <(E-Mail Removed)> wrote:
> > >
> > > > Are these programs correct ?
> > > >
> > > > #include <stdio.h>
> > > > unsigned char a[2] = {1,2};
> > > > int main(void) {
> > > > unsigned char j;
> > > > for(j=1; j<=2; ++j)
> > > > printf("%u\n", *( a+j-1 ));
> > > > return 0; }
> > > >
> > > > #include <stdio.h>
> > > > unsigned char a[2] = {1,2};
> > > > int main(void) {
> > > > unsigned char j;
> > > > for(j=1; j<=2; ++j)
> > > > printf("%u\n", *( a-1+j ));
> > > > return 0; }
> > > >
> > > >
> > > > #include <stdio.h>
> > > > unsigned char a[2] = {1,2};
> > > > int main(void) {
> > > > unsigned char j;
> > > > for(j=1; j<=2; ++j)
> > > > printf("%u\n", *( a+(j-1) ));
> > > > return 0; }
> > > >
> > > > rot-13 spoiler: bayl gur svefg bar vf pbeerpg.
> > >
> > > What's wrong with the third?

> >
> > Nothing. The spoiler was wrong..
> >
> > > The error in the second is clear;

> >
> > Yes. The first is wrong too, because a+2 is undefined.

>
> No. The first is correct.
> You are allowed to _calculate_ the address one
> past the end of an array. What you can't do is a. dereference said
> one-past array or b. calculate any addresses beyond that.
>
> This can be useful for the termination condition of loops over arrays.


The third one would have been wrong if the initial value
of j was zero. On systems where unsigned char promotes to unsigned,
(j-1) would have been UINT_MAX.

--
pete
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Usual Arithmetic Conversions-arithmetic expressions joshc C Programming 5 03-31-2005 02:23 AM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments