tried strdup() previously and just tried strlcpy().
For both I get no prototype function.
Guessing that relates to the compiler I'm using (Storm C 3.0) .
Unfortunately, strlcpy() is not an ANSI-C function but a compiler extension some compilers provide, and unfortunately, strncpy() has its quirks as mentioned by Olaf (namely, not NUL-terminating strings). strdup() is ANSI-C.
Anyhow, regardless of this: It might make a good exercise anyhow to attempt an implementation of these two functions if your compiler does not provide them. It's not really hard to do that, and unless your code is really time-critical (hint: it usually is not), your own versions may do just fine.
So really I should be using snprintf in the places where I used strcpy.
Note that this is not a 1:1 substitute. snprintf() comes from the printf() family and does "quite a lot of work", whereas strcpy() is a simple string copy without much intelligence.
snprintf() is a possible substitute for sprintf(), but to use it properly, the return code of the function should be checked as to detect cases, where the buffer would overrun. While this no longer corrupts memory (unlike sprintf()), it still means that the output it generated is probably not what you want - after all, it was truncated. A possible strategy would be to allocate a buffer given the return code of snprintf, and then use that....
Anyhow. As you see, writing good, robust C code is not exactly trivial, due to many legacies of the language (C++ is a bit better in this respect, but also more complex...).
I actually used strcpy because the tutorial I read said I shouldn't use sprintf. I only started using sprintf when I was looking for a way to write an integer into a string and that's when I started reading the reverse argument that strcpy shouldn't be used.
sprintf() can be used to convert an int to a char[] - or rather write it into a given buffer - while strcpy() certainly can't, so the purpose of the two functions is quite different. The way how C handles "strings" is quite archaic (actually, it only really has character arrays and string literals, nothing like a "string" as you might know from other higher languages).
Both arguments talked of the snprintf and strncpy but again both claimed 1 should be used and the other was bad.
It really depends on what you want...
sprintf() can trash memory if you are not careful enough, snprintf() cannot, but it has similar drawbacks: Its output (or rather, it's side-effect - the letters in the buffer, let's be precise) can be truncated, making it useless in such cases.
So for the matter of this little tutorial: On the Amiga, a
char buffer[12];
sprintf(buffer,"%d",number);
is perfectly safe, because ints are here at most 32 bits wide and hence *cannot possibly* be longer than 11 digits (plus NUL-termination gives 12).
So *in this case* we are perfectly safe. However, in other cases, it is not quite as trivial as how to compute as how long the result of sprintf() could possibly be, and in such cases,
char buffer[MAXBUFSIZE];
if (snprintf(buffer,something_complex,complex_arguments) >= MAXBUFSIZE) {
fprintf(stderr,"buffer overrun!\n");
exit(25); /* or some better mechanism how to handle this case - as in : enlarge the buffer */
}
is the better alternative. Unfortunately, it's not simple.