public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/57853] New: pointer arithmetic on arrays
@ 2013-07-08 20:03 brodhow at all2easy dot net
  2013-07-08 20:10 ` [Bug c/57853] " brodhow at all2easy dot net
                   ` (13 more replies)
  0 siblings, 14 replies; 15+ messages in thread
From: brodhow at all2easy dot net @ 2013-07-08 20:03 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57853

            Bug ID: 57853
           Summary: pointer arithmetic on arrays
           Product: gcc
           Version: 4.6.3
            Status: UNCONFIRMED
          Severity: critical
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: brodhow at all2easy dot net

This C code:

#include <stdio.h>

int main() { 
 char *arr [2][3]={{"as","df","ce"},{"me","yu","we"}};
 char *arr2 = NULL;

puts(*arr[0]);//works fine
puts(*arr[1]);//works fine
printf("%c\n",*++arr[0][0]);//works fine and prints s

printf("%s\n", *arr[0]);

int i = 0, j = 0;

 for (i=0; i<2; ++i)
   for (j=0; j<3; ++j)
     printf("%s ", arr[i][j]); 

 printf("\n");
}

outputs:

as
me
s
s
s df ce me yu we 

 where the 'a' in "as" (arr[0]) is being wiped out! After the
"printf("%c\n",*++arr[0][0]);" statement! Or, the string arr's head is being
reassigned to the new value after the "*++arr[0][0]" operation which is 's'!

 the output should be:

as
me
s
as
as df ce me yu we 

 where the 'a' in "as" is present, afterwards!

 The pointer arithmetic here to get 's' to output is producing this side effect 
of wiping out the 'a' in "as", arr[0].  Is "*++arr[0][0]" valid for getting 's'
in "as"?  If so, then this side effect is happening! For real in any legacy C
code with similar syntax!  When said C code is recompiled by the current gcc
compiler!  Note that g++ does the same thing here too, on this code.


^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2013-07-09 10:28 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-07-08 20:03 [Bug c/57853] New: pointer arithmetic on arrays brodhow at all2easy dot net
2013-07-08 20:10 ` [Bug c/57853] " brodhow at all2easy dot net
2013-07-08 20:17 ` pinskia at gcc dot gnu.org
2013-07-08 22:06 ` brodhow at all2easy dot net
2013-07-08 22:08 ` pinskia at gcc dot gnu.org
2013-07-08 22:10 ` brodhow at all2easy dot net
2013-07-08 22:13 ` brodhow at all2easy dot net
2013-07-08 22:15 ` brodhow at all2easy dot net
2013-07-08 22:18 ` pinskia at gcc dot gnu.org
2013-07-09  3:08 ` brodhow at all2easy dot net
2013-07-09  4:15 ` brodhow at all2easy dot net
2013-07-09  4:24 ` pinskia at gcc dot gnu.org
2013-07-09  6:52 ` brodhow at all2easy dot net
2013-07-09  7:15 ` schwab@linux-m68k.org
2013-07-09 10:28 ` redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).