This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c/57853] pointer arithmetic on arrays


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

Howard Brodale <brodhow at all2easy dot net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|INVALID                     |FIXED

--- Comment #12 from Howard Brodale <brodhow at all2easy dot net> ---
"store the incremented v back into v" is not quite right either. The pointer
arithmetic "*++arr[0][0]" is incrementing the head pointer of the string array
element one char past 'a' and not actually storing the shortened string back in
there.  If it was storing the shortened string there then the 'a' would be lost
or written over which it is not.

printf("%c\n",*++arr[0][0]);//works fine and prints s
printf("%c\n",*--arr[0][0]);//works fine does print a

as
me
s <- the 1st "++"; incrementing past 'a'
a <- the first "--"; dcrementing back to 'a'
as <- "as" prints now as expected
as df ce me yu we <- and here again, too, "as" is showing as expected. Take out
the "--" operation and these "as"s will be "s"s.

'a' is not lost nor written over by any storing back into v, here or no storing
is going on.  Just pointer that array arr gets moved over to point at the next
character.

char *arr2 = "this is a test\n";
int len_arr2 = 0;

len_arr2 = strlen(++arr2);
printf("length of arr2 is %i\n", len_arr2);
printf("%c\n", *arr2--);
len_arr2 = strlen(arr2);
printf("length of arr2 is %i\n", len_arr2);
puts(arr2);

outputs:

length of arr2 is 14 <- "++"; moving past the 't', one character less in string
his is a test <- 't' is missing now

length of arr2 is 15 <- "--"; moving back over the 't', one character more
this is a test <- 't' is back now; if storing in v happened then 't' would've
been lost and would be not recoverable.

Now, I have also recovered more knowledge about pointer arithmentic, with
character strings.  'a' is not being destoyed or lost in the original context.
Its just being ignored.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]