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

Sample Errata?
http://www.robotc.net/forums/viewtopic.php?f=1&t=7103
Page 1 of 1

Author:  TheKog [ Mon Oct 28, 2013 8:07 am ]
Post subject:  Sample Errata?

Not sure if there is a gathering place for errata from the samples but we found a bug in the NXTFileIOTest.c sample as I started going through them with my son.

At approx line 56:
sString = "ABC" + index; // sString is declared as "String"

fails to compile : Can't assign to 'const' qualified variable. Expression '"ABC" + index' is type 'string'

Is there a C++ style operator overloading that enables '+' for concatenation of strings? It seems this should work from what I found here - http://carrot.whitman.edu/Robots/NXT/Strings.htmhy would this be an issue with the current 3.62 out of the box?

Also tried:
sString = "ABC";
sString += index; // ERROR: Code generation for an invalid string assignment.

It was OK with:
sString = "ABC";
sString = sString + index;

Of course the following works as well:
WriteString(hFileHandle, nIoResult, sString);

What is going on here? Also I could not find anything on the native String support in the docs that come with RObotC or online other than the whitman doc listed above. Where is it?

Author:  Ernest3.14 [ Mon Oct 28, 2013 8:02 pm ]
Post subject:  Re: Sample Errata?

I think this is a known bug; there was a thread on this a while back. Should be fixed in 4.0.

Author:  TheKog [ Mon Oct 28, 2013 9:50 pm ]
Post subject:  Re: Sample Errata?

OK Thanks Ernest - could you point me to the documentation on the native String support? Could not find it in the docs etc other than Whitman site.

Author:  MHTS [ Mon Oct 28, 2013 9:58 pm ]
Post subject:  Re: Sample Errata?

You mean you don't get this?

Attachments:
Capture.JPG
Capture.JPG [ 41.56 KiB | Viewed 4947 times ]

Author:  TheKog [ Mon Oct 28, 2013 10:07 pm ]
Post subject:  Re: Sample Errata?

I got that, but where is the description for the String data type, the operators, and extension to the language. Those are merely functions that operate on strings. Generally where is THE the language definition for RobotC? Not the function library but the language.

Author:  MHTS [ Mon Oct 28, 2013 10:20 pm ]
Post subject:  Re: Sample Errata?

To be honest with you, I don't use the "string" data type in RobotC. I think it has a limit of storing only 19 characters. I am not 100% sure. I haven't played with string for a long time, but that was the impression I got when I played with it long time ago. It seems pretty useless to me. Since 3.x now supports pointers, I use char array (and char *) instead. Having said that, char array also has a limit of about 220 characters. I have already filed a bug against it because I ran into that limitation when writing a program that requires long strings.

Author:  TheKog [ Tue Oct 29, 2013 8:05 am ]
Post subject:  Re: Sample Errata?

Yes that is how it would be done in "real" C ;) Is it strict on data typing? For example if a function expects a "String" type can you pass it something declared "char *" - does RobotC allow "casting" etc?

Author:  MHTS [ Tue Oct 29, 2013 8:57 am ]
Post subject:  Re: Sample Errata?

TheKog wrote:
Yes that is how it would be done in "real" C ;) Is it strict on data typing? For example if a function expects a "String" type can you pass it something declared "char *" - does RobotC allow "casting" etc?

If you are talking about the supported string related functions such as strcpy, strcat ... etc, they supported both "string" and "char *" types. For example, take a look at the file c:\Program Files (x86)\Robomatter Inc\ROBOTC Development Environment\Includes\RobotCIntrinsics.c
Code:
///////////////////////////////////////////////////////////////////////////////////////////////////////
//
//                                "string" functions on byte arrays
//
///////////////////////////////////////////////////////////////////////////////////////////////////////

#if defined(hasStringSupport)

intrinsic void strcpy(char *pToBuffer, const char *pFromBuffer)
                      asm(opcdSystemFunctions,
                          byte(sysFuncStrCpy),
                          variableRefCharPtr(pToBuffer),
                          variableRefCharPtr(pFromBuffer));

intrinsic void strcpy(string &pToBuffer, const char *pFromBuffer)
                      asm(opcdSystemFunctions,
                          byte(sysFuncStrNCpy),
                          variableRefString(pToBuffer),
                          variableRefCharPtr(pFromBuffer),
                          constantValue(20));

intrinsic void strncpy(char *pToBuffer, const char *pFromBuffer, const short nMaxBufferSize)
                      asm(opcdSystemFunctions,
                          byte(sysFuncStrNCpy),
                          variableRefCharPtr(pToBuffer),
                          variableRefCharPtr(pFromBuffer),
                          variable(nMaxBufferSize));

intrinsic void strcat(string &sToString, const char *pFromBuffer)
                      asm(opcdSystemFunctions, byte(sysFuncStrNCat),
                            variableRefString(sToString),
                            variableRefCharPtr(pFromBuffer),
                            constantValue(20));   // 'string' variable is 20 bytes long

intrinsic void strcat(char *pToBuffer, const char *pFromBuffer)
                      asm(opcdSystemFunctions, byte(sysFuncStrCat),
                            variableRefCharPtr(pToBuffer),
                            variableRefCharPtr(pFromBuffer));

intrinsic void strncat(char *pToBuffer, const char *pFromBuffer, const short nMaxBufferSize)
                      asm(opcdSystemFunctions,
                          byte(sysFuncStrNCat),
                          variableRefCharPtr(pToBuffer),
                          variableRefCharPtr(pFromBuffer),
                          variable(nMaxBufferSize));

intrinsic void strcatSingleChar(char &pToBuffer, const char cSingleChar)
                      asm(opcdSystemFunctions, byte(sysFuncStrCatSingleChar),
                            variableRefRAM(pToBuffer),
                            variable(cSingleChar));

intrinsic void strncatSingleChar(char *pToBuffer, const char cSingleChar, const short nMaxBufferSize)
                      asm(opcdSystemFunctions,
                          byte(sysFuncStrNCatSingleChar),
                          variableRefCharPtr(pToBuffer),
                          variable(cSingleChar),
                          variable(nMaxBufferSize));

intrinsic short strcmp(const char *pString1, const char *pString2)
                      asm(opcdSystemFunctions, byte(sysFuncStrCmp),
                            variableRefCharPtr(pString1),
                            variableRefCharPtr(pString2),
                            functionReturn);

intrinsic short strncmp(const char *pString1, const char *pString2, const short nMaxBufferSize)
                      asm(opcdSystemFunctions, byte(sysFuncStrNCmp),
                            variableRefCharPtr(pString1),
                            variableRefCharPtr(pString2),
                            variable(nMaxBufferSize),
                            functionReturn);

intrinsic char strIndex(const string &sString, const short nIndex)
                      asm(opcdSystemFunctions,
                          byte(sysFuncStrIndex),
                          variableRefString(sString),
                          variable(nIndex),
                          functionReturn);


intrinsic void strTrim(string &sString)
                      asm(opcdSystemFunctions,
                          byte(sysFuncStrNTrim),
                          variableRefString(sString),
                                       byte(parmSourceConstant), byte(20), byte(0));

intrinsic void strTrim(char *pString)
                      asm(opcdSystemFunctions,
                          byte(sysFuncStrTrim),
                          variableRefCharPtr(pString));

intrinsic void strTrimN(char *pString, const int nMaxBufferSize)
                      asm(opcdSystemFunctions,
                          byte(sysFuncStrNTrim),
                          variableRefCharPtr(pString),
                                       variable(nMaxBufferSize));


#endif

If you are talking about your own functions, RobotC does do type checking, but it will do automatic type conversion in some cases. For example, try the following code:
Code:
void myFunc(string &myStr)
{
    nxtDisplayTextLine(0, "%s", myStr);
}

task main()
{
    char *textString = "Hello World!";
    myFunc(textString);
    while (true)
    {
        wait1Msec(100);
    }
}

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