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