Re: String Manipulation Functions - strcpy, strncpy
1. Can someone tell me the difference between these two functions:
void strcpy(char *s1, const char *s2)
while(*s1++ = *s2++)
//function prototype of strcpy follows
char *strcpy(char *s1, const char *s2) // library function
Both functions are doing the same job.
Why the later function has to return a char pointer?
We call both functions in the same manner and we don't sssign the
return pointer to any other pointer. Correct me if I'm wrong.
C and C++ allow ignoring thee return value of a function, returning
a char * allows chaining of functions together which can be important,
as in this C code [or C++ code using nothing but C]
char * combine(const char *p,const char *q)
unsigned long new_size = strlen(p)+strlen(q)+1;
char *out = malloc(new_size);
in C++ I use std::string unless there is a reasoon not, definitely not
this code without guarding ageninst leaks, the C++ code I'd use if I was
given two C strings is probably
intine std::string combine(const char *p,const char *q)
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:firstname.lastname@example.org ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]