what does [-1] mean?

  • Follow


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! ]
0
Reply toddmarshall2002 10/20/2004 4:21:01 PM

d will have a value of 'h'. Square brackets basically do the same as
pointer arithmetic, ie:

abc[-1] is equivalent to *(abc - 1)

toddmarshall2002@yahoo.com wrote:
> 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! ]

0
Reply John 10/21/2004 4:02:59 PM


toddmarshall2002@yahoo.com wrote:
> is this valid? what does it mean?

Yes it is.
>
> 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.

> abc++;

Adds one to the pointer, *abc now is 'e'

>
> char d = abc[-1];

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! ]

0
Reply Ron 10/21/2004 4:03:28 PM

<toddmarshall2002@yahoo.com> schrieb im Newsbeitrag 
news:c7aadd5c.0410190529.72bf716c@posting.google.com...
> 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! ]
0
Reply Heinz 10/21/2004 4:16:09 PM

toddmarshall2002@yahoo.com wrote:
> 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]

> char *abc = "hello";

This sets abc to point to the first character in the character array
"hello", the 'h'.

> abc++;

This makes abc point to the next element in the array it is pointing to.
    So now abc is pointing to the 'e'.

> char d = abc[-1];

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! ]
0
Reply M 10/21/2004 4:37:06 PM

On 20 Oct 2004 12:21:01 -0400, toddmarshall2002@yahoo.com wrote:

> 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
NoSpamPlease@Home.com

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Bob 10/21/2004 4:42:33 PM

<toddmarshall2002@yahoo.com> wrote in message
news:c7aadd5c.0410190529.72bf716c@posting.google.com...

> 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.

> does it work?

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! ]

0
Reply Balog 10/21/2004 4:43:17 PM

toddmarshall2002 wrote:

> is this valid? what does it mean?
>
> char *abc = "hello";

That should be char const *

> abc++;
>
> char d = abc[-1];

It means this passes:

    assert('h' == d);

> does it work?

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! ]

0
Reply Phlip 10/21/2004 5:07:09 PM

toddmarshall2002@yahoo.com wrote:
 > 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! ]
0
Reply Chris 10/21/2004 5:20:01 PM

On 20 Oct 2004 12:21:01 -0400, toddmarshall2002@yahoo.com wrote:

 >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! ]
0
Reply bk039 10/21/2004 5:23:59 PM

Todd asked:

 > 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! ]
0
Reply David 10/21/2004 5:26:12 PM

<toddmarshall2002@yahoo.com> wrote in message
news:c7aadd5c.0410190529.72bf716c@posting.google.com...
 > 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.

 > does it work?

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! ]
0
Reply Ivan 10/21/2004 5:28:17 PM

On 20 Oct 2004 12:21:01 -0400, toddmarshall2002@yahoo.com wrote in
comp.lang.c++.moderated:

 > is this valid? what does it mean?
 >
 > char *abc = "hello";

This defines an unnamed array of 6 constant characters, 'h', 'e', 'l',
'l', 'o', '\0'.  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.

 > abc++;

This advances the pointer by one byte, so it now points to the second
character, the 'e', in the string literal.

 > char d = abc[-1];

This reads the value of the character before the 'e' that abc points
to, the 'h'.

 > does it work?

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.

 > thanks
 > Todd.

-- 
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! ]
0
Reply Jack 10/21/2004 5:29:44 PM

toddmarshall2002@yahoo.com wrote:
 > 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
NO.antounk.SPAM@comcast.net

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Antoun 10/21/2004 5:38:47 PM

toddmarshall2002@yahoo.com wrote in message news:<c7aadd5c.0410190529.72bf716c@posting.google.com>...
 > 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! ]
0
Reply jtorjo 10/21/2004 5:39:26 PM

"Antoun Kanawati" <NO.antounk.SPAM@comcast.net> wrote in message 
news:nVJdd.432324$Fg5.157704@attbi_s53...
> toddmarshall2002@yahoo.com wrote:
> > 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.
>

Why do you think it's undefined?
I believe it is pretty much defined by the standard (paragraph 8.3.4 (6)).

Ivan

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Ivan 10/22/2004 5:40:05 PM

Ivan Krivyakov wrote:
 >>toddmarshall2002@yahoo.com wrote:
 >>>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 behavior.
 >>
 > Why do you think it's undefined?
 > I believe it is pretty much defined by the standard (paragraph 8.3.4 (6)).

Oooooops.  I missed the abc++ line.

Yes, in this case, this is quite well defined.  I was seeing:

     char *abc = "hello";  // no abc++;
     char d = abc[-1];

when I answered.

When you consider the 'abc++' statement, abc[-1] refers to the 'h' in
"hello".

My apologies.

This may be a good time to consider normal sleeping hours :)
-- 
A. Kanawati
NO.antounk.SPAM@comcast.net

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Antoun 10/24/2004 4:04:46 AM

16 Replies
83 Views

(page loaded in 0.164 seconds)

Similiar Articles:


















7/25/2012 3:36:20 AM


Reply: