public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "brodhow at all2easy dot net" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/57853] pointer arithmetic on arrays
Date: Tue, 09 Jul 2013 06:52:00 -0000	[thread overview]
Message-ID: <bug-57853-4-BCsiHKXDEJ@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-57853-4@http.gcc.gnu.org/bugzilla/>

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.


  parent reply	other threads:[~2013-07-09  6:52 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-08 20:03 [Bug c/57853] New: " 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 [this message]
2013-07-09  7:15 ` schwab@linux-m68k.org
2013-07-09 10:28 ` redi at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-57853-4-BCsiHKXDEJ@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).