ROBOTC.net forums
http://www.robotc.net/forums/

Accessing individual string characters
http://www.robotc.net/forums/viewtopic.php?f=1&t=5008
Page 1 of 1

Author:  brianchen11 [ Sun Oct 21, 2012 1:43 am ]
Post subject:  Accessing individual string characters

Hi,

I have always been able to access individuals characters in a string in c++ like this.

string str = "hello";
char c = str[2];

However, when I try this in RobotC, it comes up with the error:
**Error**:LValue for '[]' operator must be a pointer
**Error**:'[]' operator requires pointer value on left hand side [2].

How would I do this in RobotC?

Author:  MHTS [ Sun Oct 21, 2012 6:33 am ]
Post subject:  Re: Accessing individual string characters

Code:
task main()
{
    char str[] = "hello";
    char c = str[2];
    nxtDisplayTextLine(0, "c=%c", c);
}

Author:  brianchen11 [ Sun Oct 21, 2012 7:00 pm ]
Post subject:  Re: Accessing individual string characters

Thanks.

So just don't use the "string" type but an array of chars.
I did it in the end by copying the string into an array using memcpy.

Author:  MHTS [ Sun Oct 21, 2012 11:42 pm ]
Post subject:  Re: Accessing individual string characters

I don't really understand nor am interested in how string in RobotC works now that it supports pointer syntax. So yes, just use array of char and forget about string. Note that if I recall correctly, string is only good for up to 19 characters anyway. So I don't see any good reason to use string at all. In fact, I think the only reason string still exist in RobotC is for backward compatibility.

Author:  tfriez [ Thu Oct 25, 2012 12:29 pm ]
Post subject:  Re: Accessing individual string characters

That pretty much sums it up - We like the character arrays much more than Strings, but so many people are used to the old implementation so we've worked (much to our pain) to make sure everything will work with both Char Arrays and Strings.

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/