 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
toddmarshall2002@yahoo.co Guest
|
Posted: Wed Oct 20, 2004 4:21 pm Post subject: what does [-1] mean? |
|
|
is this valid? what does it mean?
char *abc = "hello";
abc++;
char d = abc[-1];
does it work?
thanks
Todd.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
John Snelson Guest
|
Posted: Thu Oct 21, 2004 4:02 pm Post subject: Re: what does [-1] mean? |
|
|
d will have a value of 'h'. Square brackets basically do the same as
pointer arithmetic, ie:
abc[-1] is equivalent to *(abc - 1)
[email]toddmarshall2002 (AT) yahoo (DOT) com[/email] wrote:
| Quote: | is this valid? what does it mean?
char *abc = "hello";
abc++;
char d = abc[-1];
does it work?
thanks
Todd.
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Thu Oct 21, 2004 4:03 pm Post subject: Re: what does [-1] mean? |
|
|
[email]toddmarshall2002 (AT) yahoo (DOT) com[/email] wrote:
| Quote: | is this valid? what does it mean?
|
Yes it is.
| Quote: |
char *abc = "hello";
|
This creates a pointer to char, it creates an six element
array of const char in some unspecified static location with
the value "hello" (including a null terminator) in it. It
uses a deprecated conversion to char* to initialize the pointer.
Adds one to the pointer, *abc now is 'e'
abc[-1] is defined to be the same as *(abc + -1)
The value is 'h'. (abc + -1) is the original
beginning of the array.
Note that when doing math on pointers, once you
move the pointer value outside of the array it
originally pointed to (with the exception of one
past the end), you have undefined behavior.
That doesn't happen here, but the opposite:
char* abc = "hello";
abc--;
char d = abc[1]
is undefined behavior.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Heinz Ozwirk Guest
|
Posted: Thu Oct 21, 2004 4:16 pm Post subject: Re: what does [-1] mean? |
|
|
<toddmarshall2002 (AT) yahoo (DOT) com> schrieb im Newsbeitrag
news:c7aadd5c.0410190529.72bf716c (AT) posting (DOT) google.com...
| Quote: | is this valid? what does it mean?
char *abc = "hello";
abc++;
char d = abc[-1];
does it work?
|
For built-in types a[b] is equivalent to *(a + b). So abc[-1] is valid
as long as abc points at least one item after the start of some array.
You can even write (-1)[abc].
HTH
Heinz
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
M Jared Finder Guest
|
Posted: Thu Oct 21, 2004 4:37 pm Post subject: Re: what does [-1] mean? |
|
|
[email]toddmarshall2002 (AT) yahoo (DOT) com[/email] wrote:
| Quote: | is this valid? what does it mean?
|
Yes, it does work, and will set d to the character 'h'. Remember that
for pointers, a[b] refers to the value at an offset b from where a is
pointing.[1]
| Quote: | char *abc = "hello";
|
This sets abc to point to the first character in the character array
"hello", the 'h'.
This makes abc point to the next element in the array it is pointing to.
So now abc is pointing to the 'e'.
This says "get the character 1 before wherever abc is pointing. This is
the 'h' at the start of the string.
[1] Technically on pointers a[b] is equivalent to *(a + b), which is
equivalent to *(b + a) which is equivalent to b[a]. Common style
dictates that the pointer should be outside the subscript, making the
statement true as long as you follow that style.
-- MJF
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Bob Hairgrove Guest
|
Posted: Thu Oct 21, 2004 4:42 pm Post subject: Re: what does [-1] mean? |
|
|
On 20 Oct 2004 12:21:01 -0400, [email]toddmarshall2002 (AT) yahoo (DOT) com[/email] wrote:
| Quote: | is this valid? what does it mean?
char *abc = "hello";
abc++;
char d = abc[-1];
does it work?
thanks
Todd.
|
abc should be declared as char const *, otherwise the code looks valid
to me.
The C++ standard (see 8.3.4.6) seems to allow the syntax "abc[-1]"
since abc[n], where n is an integer, is equivalent to *(abc + n).
Since the above code pre-increments the pointer abc by one, the
character variable d should contain 'h' after your code snippet runs.
Whether or not this is good coding practice, however, is another
story.
--
Bob Hairgrove
[email]NoSpamPlease (AT) Home (DOT) com[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Balog Pal Guest
|
Posted: Thu Oct 21, 2004 4:43 pm Post subject: Re: what does [-1] mean? |
|
|
<toddmarshall2002 (AT) yahoo (DOT) com> wrote
| Quote: | is this valid? what does it mean?
char *abc = "hello";
abc++;
char d = abc[-1];
|
a[b] is just syntax sugar for *(a+b).
so abc[-1] or (-1)[abc] both mean *(abc-1), what will yield 'h' in the
example.
Sure, as long as pointer math is legal, iow as long as the result is
still
within the array.
Paul
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Phlip Guest
|
Posted: Thu Oct 21, 2004 5:07 pm Post subject: Re: what does [-1] mean? |
|
|
toddmarshall2002 wrote:
| Quote: | is this valid? what does it mean?
char *abc = "hello";
|
That should be char const *
| Quote: | abc++;
char d = abc[-1];
|
It means this passes:
assert('h' == d);
Yep. A pointer is valid if it points into an array (or off the end of an
array by one). Dereferencing a pointer is valid if it points at a valid
object.
An array subscript is the same as pointer arithmetic followed by
dereferencing. These are the same:
abc[1];
*(abc + 1);
So, put them all together, and *(abc - 1) is well-formed and well-defined,
so abc[-1] is too.
Please don't ever do it!
--
Phlip
http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Chris Soanes Guest
|
Posted: Thu Oct 21, 2004 5:20 pm Post subject: Re: what does [-1] mean? |
|
|
[email]toddmarshall2002 (AT) yahoo (DOT) com[/email] wrote:
| Quote: | is this valid? what does it mean?
char *abc = "hello";
abc++;
char d = abc[-1];
does it work?
|
Yes, it will work. abc effectively a pointer, and abc[0] is the contents
of the memory at the location pointed to at abc, in this case the
charachter 'h'. abc++ will increment the pointer by one which (assuming,
and it's an assumption of unknown size...., but assuming that your
implementation stores arrays as contiguous blocks of memory) should then
point abc the next item in the array. Therefore abc[0] is 'e', abc[-1]
might, if you're lucky, point to 'h'.
Note all the occurrences of 'might', 'should', and 'if' in the above
paragraph. If you really need to use this idiom, make sure you know
everything there is to now about the compiler you're using, and every
other compiler that anyone might possibly use in the future to compile
this code.
Tiff
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Raymond Martineau Guest
|
Posted: Thu Oct 21, 2004 5:23 pm Post subject: Re: what does [-1] mean? |
|
|
On 20 Oct 2004 12:21:01 -0400, [email]toddmarshall2002 (AT) yahoo (DOT) com[/email] wrote:
| Quote: | is this valid? what does it mean?
char *abc = "hello";
abc++;
char d = abc[-1];
|
It's valid, but using this feature of C++ should be avoided. It's
generally better to create a secondary pointer to traverse the string and
keep the original pointer when you have to start from the beginning.
The [-1] in the array access the element one step before the current
position of the pointer. In this case, *abc points to the second element
to produce the string "ello" (it advanced by one letter). Accessing the
element before the pointer would provide the letter 'h'.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
David Fisher Guest
|
Posted: Thu Oct 21, 2004 5:26 pm Post subject: Re: what does [-1] mean? |
|
|
Todd asked:
| Quote: | is this valid? what does it mean?
char *abc = "hello";
abc++;
char d = abc[-1];
does it work?
|
The last line is identical in meaning to:
char d = *(abc - 1);
which sets d to the character 'h'. If this seems weird, just think of how it
works with a positive index - eg. abc[3] == *(abc + 3)
BTW, char *abc should really be const char *abc - as far as I know, the fact
that a literal string like "hello" can be assigned to a char* is for
compatibility with C.
David Fisher
Sydney, Australia
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ivan Krivyakov Guest
|
Posted: Thu Oct 21, 2004 5:28 pm Post subject: Re: what does [-1] mean? |
|
|
<toddmarshall2002 (AT) yahoo (DOT) com> wrote
| Quote: | is this valid? what does it mean?
char *abc = "hello";
abc++;
char d = abc[-1];
|
According to C++ standard (paragraph 8.3.4 (6)),
x[y] is equivalent to *(x+y), except when operator[]
is invoked for a class.
Thus, abc[-1] should mean the same as *(abc-1) - the item
that is just before the one to which abc points.
I think with standard conforming compiler it should.
However, this is probably one of those "don't try this at home" things.
Only actual experiment can verify whether it works with *your* compiler.
Ivan
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Jack Klein Guest
|
Posted: Thu Oct 21, 2004 5:29 pm Post subject: Re: what does [-1] mean? |
|
|
On 20 Oct 2004 12:21:01 -0400, [email]toddmarshall2002 (AT) yahoo (DOT) com[/email] wrote in
comp.lang.c++.moderated:
| Quote: | is this valid? what does it mean?
char *abc = "hello";
|
This defines an unnamed array of 6 constant characters, 'h', 'e', 'l',
'l', 'o', ' '. It also defines a pointer to non-constant char,
'abc', and sets it to point to the first character, the 'h', of the
unnamed array of constant characters. C++ makes an exception in its
typing system to allow the address of a string literal to be assigned
to a pointer to non-constant char, for backwards compatibility with C
code.
This advances the pointer by one byte, so it now points to the second
character, the 'e', in the string literal.
This reads the value of the character before the 'e' that abc points
to, the 'h'.
It works and it is perfectly legal to use a negative index from a
pointer into an array, as long as the result does not go past the
beginning of the array.
So abc[-2] would produce undefined behavior.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Antoun Kanawati Guest
|
Posted: Thu Oct 21, 2004 5:38 pm Post subject: Re: what does [-1] mean? |
|
|
[email]toddmarshall2002 (AT) yahoo (DOT) com[/email] wrote:
| Quote: | is this valid? what does it mean?
char *abc = "hello";
abc++;
char d = abc[-1];
does it work?
|
abc[-1] generates a reference to the character before 'h' in "hello".
whether it "works" or not depends on your definition of "works".
It's undefined behavio.
--
A. Kanawati
[email]NO.antounk.SPAM (AT) comcast (DOT) net[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
John Torjo Guest
|
Posted: Thu Oct 21, 2004 5:39 pm Post subject: Re: what does [-1] mean? |
|
|
[email]toddmarshall2002 (AT) yahoo (DOT) com[/email] wrote in message news:<c7aadd5c.0410190529.72bf716c (AT) posting (DOT) google.com>...
| Quote: | is this valid? what does it mean?
char *abc = "hello";
abc++;
char d = abc[-1];
does it work?
|
It's valid, and it works. It returns 'h'.
abc[-1] is equivalent to *(abc-1)
So, if abc points in the middle of a string (and not at the beginning
of it), it's perfectly valid.
Best,
John
John Torjo, Contributing editor, C/C++ Users Journal
-- "Win32 GUI Generics" -- generics & GUI do mix, after all
-- http://www.torjo.com/win32gui/
-- v1.5 - tooltips at your fingertips (work for menus too!)
+ bitmap buttons, tab dialogs, hyper links, lite html
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|