Re: String Manipulation Functions - strcpy, strncpy
<chikito.chikito@gmail.com> wrote:
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);
if(out)
strcat(strcpy(out,p),q);
return out;
}
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)
{
std::string out(p);
return out.append(q);
}
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]