Is this portable?

VISINFO Moderators visinfo at ethz.UUCP
Fri May 12 23:02:10 AEST 1989


In Article 15145 of comp.lang.c Evan writes 

>This works on my machine, but I am curious as to whether it is "officially"
>correct and portable C.  I can think of some machines where it could be 
>trouble
>
>	main()
>	  { int array[10], *ptr, j;
>
>	    for(j=0; j<10; j++)
>	      array[j]=j;
>
>	    ptr= &(array[1]);
>
>	    for(j= -1; j<9; j++)
>		printf("%d\n", ptr[j]);
>	  }

I suppose this to be (rather) portable, 'cause the compiler takes the adress 
of 'ptr', adds j*sizeof(int) and writes out the content of the memory location 
this pointer is pointing to. So when K&R says : pointer+int is OK : , let us
use it. Just imagine the following source (travelling in a string):

{
char c[]= "abc";
char *runner;

runner=&(c[2]);

while (runner!=c)
  runner--;
}

I see no dangers in it (it's just another way to confuse any kind of range-
checking facility) ! 

					Tony



More information about the Comp.lang.c mailing list