And with a bit of namespacing and/or object orientation and usage of dots, it becomes perfectly readable.
There are also camel case and underscores in other languages…
BTW: How on earth should a newcomer know that the letter “n” in that word stands for number without having to google it? The newcomer could even assume that it’s a letter of the word string… And even, if you know that it stands for number, it’s still hard for me to understand what it means in this context… I actually had to google it… But that’s probably some C++ convention I don’t know about, because I don’t program in C++…
How on earth should a newcomer know that the letter “n” in that word stands for number without having to google it?
By looking at the difference between strcpy and strncpy. Preferably, though, you should simply learn C before writing C.
The gist of is is that strcpy takes a null-terminated string and copies it somewhere, while strncpy takes a zero-terminated string and copies it somewhere but will not write more than n bytes. strncpy literally has exactly one more parameter than strcpy, that being n, hence the name. If n is smaller than the string length (as in: distance to first null byte) then you’re bound to have garbage in your destination, and to check for that you have to dereference the pointer strncpy returns and check if it’s actually null. Yay C error handling.
In retrospect null-terminated strings were a mistake, but so were many other things, at some point you just have to accept that there’s hysterical raisins everywhere.
If n is smaller than the string length (as in: distance to first null byte) then you’re bound to have garbage in your return destination
Wha? N is just maximum length of string to copy. Data after dst+n is unchanged.
In retrospect null-terminated strings were a mistake, but so were many other things, at some point you just have to accept that there’s hysterical raisins everywhere.
Sure but that means the part before that is garbage because you have a null terminated string without terminator.
Or at least that’s how I see it. If your intention isn’t to start and end with a null-terminated string you should be using memcpy. Let us not talk about situations where CHAR_BIT != 8 that’s not POSIX anyway.
Even better, just avoid doing string manipulation in C.
strncpy becomes stringnumbercopy. You can see why short version is used.
And with a bit of namespacing and/or object orientation and usage of dots, it becomes perfectly readable.
There are also camel case and underscores in other languages…
BTW: How on earth should a newcomer know that the letter “n” in that word stands for number without having to google it? The newcomer could even assume that it’s a letter of the word string… And even, if you know that it stands for number, it’s still hard for me to understand what it means in this context… I actually had to google it… But that’s probably some C++ convention I don’t know about, because I don’t program in C++…
By looking at the difference between
strcpy
andstrncpy
. Preferably, though, you should simply learn C before writing C.The gist of is is that
strcpy
takes a null-terminated string and copies it somewhere, whilestrncpy
takes a zero-terminated string and copies it somewhere but will not write more thann
bytes.strncpy
literally has exactly one more parameter thanstrcpy
, that beingn
, hence the name. Ifn
is smaller than the string length (as in: distance to first null byte) then you’re bound to have garbage in your destination, and to check for that you have to dereference the pointerstrncpy
returns and check if it’s actually null. Yay C error handling.In retrospect null-terminated strings were a mistake, but so were many other things, at some point you just have to accept that there’s hysterical raisins everywhere.
Wha? N is just maximum length of string to copy. Data after dst+n is unchanged.
All hail length-prefixed strings!
Sure but that means the part before that is garbage because you have a null terminated string without terminator.
Or at least that’s how I see it. If your intention isn’t to start and end with a null-terminated string you should be using memcpy. Let us not talk about situations where
CHAR_BIT != 8
that’s not POSIX anyway.Even better, just avoid doing string manipulation in C.
Yeah, let’s not talk about 20-bit one’s complement ints.
man strncpy
Removed by mod
Why not just add function overloading to the language and have a function named
copy
that takes a string and an optional character count?