I was thinking of making a strncpy alternative with terminating '\0' for my own use in a header file, and am wondering what of the following approaches would be better.
int copystring(char *dest,char *source,int elements)
{
int run;
for(run=0;run<elements-1;run++)//last reserved for'\0'
{
dest[run]=source [run];
if (dest[run]=='\0')
{
break;
}
}
dest[elements-1]='\0';//could make conditional but not neccesary.
return 0;
}
OR
int copystring(char *dest,char *source,int elements)
{
strncpy(dest,source,elements-1);
dest[elements-1]='\0';
return 0;
}
The obvious difference to me is that I'm calling one less function with version one, but I was wondering if version two has any advantages, as I don't know the internal workings of strncpy()
As an aside, why does strncpy() take size_t as the final argument? if it's for passing sizeof values, which will only be useful in very limited circumstances, wouldn't an int do just as well?