3 ms·
There is one, and it exist inside the gnu libc. strndup performs identical to strncpy except that it always terminates the destination string. (https://www.gnu
by belorn 11y ago
There is one, and it exist inside the gnu libc.
strndup performs identical to strncpy except that it always terminates the destination string. (https://www.gnu.org/software/libc/manual/html_node/Copying-and-Concatenation.html https://www.gnu.org/software/libc/manual/html_node/Copying-a...)
- jpollock 11y agostrndup is not a replacement for strncpy, it allocates memory, whereas strncpy takes an existing buffer. In-place memory usage is very important for preparing anything of a fixed size (structs) to be written out to disk or the network.
- JulianMorrison 11y agoYou remember the Holy Grail scene from Indiana Jones? The problem is not the absence of the grail, somewhere on the shelf. The problem is all the others and the fact that if you pick the wrong one, your program desiccates, crumbles to dust, and is blown away by a conveniently dramatic wind. A grail shelf that wasn't intended as a booby trap would contain one cup, the right one.
- pjc50 11y agoBut this is the great advantage of C! It's one Holy Grail for every platform! Choice is wonderful! /sarcasm