NXC: strncpy

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

NXC: strncpy

Post by HaWe »

the NXC help says:

Code: Select all

NXC: strncpy
string strncpy  ( string &  dest,  
  const string &  src,  
  unsigned int  num  
 ) 
so strncpy has a string return value ? (not void?)
AND
additionally has a pass-by-reference & dest ?
And why is src referenced (there's no need to change it) - although it's a constant?

why that?

though it equals the C def in some way... but I don't understand it...if it's changing the referenced value, why does it need a return value?
strncpy
<cstring>

char * strncpy ( char * destination, const char * source, size_t num );

Copy characters from string
Copies the first num characters of source to destination. If the end of the source C string (which is signaled by a null-character) is found before num characters have been copied, destination is padded with zeros until a total of num characters have been written to it.

No null-character is implicitly appended at the end of destination if source is longer than num (thus, in this case, destination may not be a null terminated C string).

destination and source shall not overlap (see memmove for a safer alternative when overlapping).

Parameters

destination
Pointer to the destination array where the content is to be copied.
source
C string to be copied.
num
Maximum number of characters to be copied from source.
size_t is an unsigned integral type.


Return Value
destination is returned.
spillerrec
Posts: 358
Joined: 01 Oct 2010, 06:37
Location: Denmark
Contact:

Re: NXC: strncpy

Post by spillerrec »

It is not needed, but it can be quite convenient as you can pass it directly into another function.

Code: Select all

TextOut( 0,0, strncpy( a, b, x ) );
That is why many functions which works directly on the input parameter also returns it, to provide more flexibility in its use.
My blog: http://spillerrec.dk/category/lego/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: strncpy

Post by HaWe »

ok, that destination-string thing makes sence, thank you!

But I'm still curious about that constant-source-by-reference - thing... :?
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC: strncpy

Post by afanofosc »

I think that is intended to mimic the definition of strncpy in the C standard but that in NXC it has no effect -

Code: Select all

inline string strncpy(string & dest, const string & src, unsigned int num) {
  asm {
    strsubset dest, src, 0, num
    mov __STRBUFFER__, dest
  }
}
If I recall correctly, a const reference type allows you to pass temporaries (i.e., messages where messages is an array of string). This would definitely not be allowed for a non-const reference. It could just as well be "const string src" rather than "const string & src" but then it would "look" a bit more different in terms of its signature from what the C standard function looks like.

Code: Select all

task main()
{
  string messages[] = {"hello", "world"};
  string dest;
  NumOut(0, LCD_LINE1, strncpy(dest, messages[0], 3), false);
}
As you know, NXC reference type are not true references so functionally "const string src" vs "const string & src" is identical (iirc)

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: strncpy

Post by HaWe »

If I recall correctly, a const reference type allows you to pass temporaries (i.e., messages where messages is an array of string). This would definitely not be allowed for a non-const reference.

I don't quite understand the meaning of " temporaries"...:
temporary opposite to what? (a variable is always "temporary" by it's value in some kind of way)
Does this mean that

Code: Select all

string strncpy ( string  &dest, string src,  unsigned int  num ) 
would be invalid or work different?
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC: strncpy

Post by afanofosc »

As mentioned, since NXC references are still "pass by value" in real life rather than "pass by reference" in real C then declaring a const & parameter in an NXC function is equivalent to a const non-reference. i.e., const string src is equivalent to const string & src. The "const" bit will be enforced by the compiler to the extent that you will not be able to assign a new value to that parameter inside your function. i.e., you won't be allowed to write src = "foo"; inside your function if you declare it as "const". A non-const reference declaration will not let you pass in an expression. It requires a variable since it attempts to copy back out the value of that parameter to the argument you pass in. I.e. ,what strncpy does to string & dest. It modifies that value so you could not pass in a const string "foo" or a function call that returns a string or an array of strings index into one of the string values.

Code: Select all

string foo(string & x) { return x + "foo"; }

task main()
{
  string tmp;
  string messages[] = {"amazing", "grace"};
  foo("tough");
  foo(strcpy(tmp, "foo"));
  foo(messages[0]);
}
All of these (in NXC) are invalid attempts to call foo since they involve temporaries. In real C you can do this sort of thing in some (maybe all) cases. But they would all work fine in NXC if you added "const" to the function declaration (i.e., const string & x) OR if you changed x to not be a reference type.

The only reason I used const string & instead of just string or const string was because to me it looked more similar to the standard C declaration which is const char * src. Since NXC does not support pointers the only way to mimic char * is via string &.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: strncpy

Post by HaWe »

thx,
the &var thing vs. just var was clear.
If it was &var the value of the calling function might be changed,
If it was just var the value of the calling function always will remain the same.
(That &var is faked is secondary at this moment. )

In my understanding a "const" was something completely different: a value that might be never ever changed by what-ever,
e.g.
const int value=42;
would be exchanged already by the preprocessor by "42" at every place all over in the code, just like
#define value 42
And so a const passed as "const string &var" appeared to be a contradiction in itself.

But if
const string &var
is exactly the same as
string var
then I now understand what this means - at first glance, it's a little bit confusing nevertheless .

EDIT:
the original
char * source
thing was in my understanding just for speed reasons - one doesn't have to copy and pass a ton of kbytes for the string itself but just a handful of bytes for it's address to refer to
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests