Topic: String Manipulation Functions - strcpy, strncpy


Author: chikito.chikito@gmail.com
Date: Sun, 1 Apr 2007 11:23:14 CST
Raw View
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.

-------------------------
2. This question is regarding strncpy library function.

//function prototype of strncpy follows
char *strncpy(char *s1, const char *s2, size_t n);
We need to pad '\0' to the end of s1 when n is less than the length of
s2 plus one.

Compare strncpy with strncat. The function strncat pad '\0' by itself.
Why this was left to the programmer in the case of strncpy function.

---
[ 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                      ]





Author: cbarron3@ix.netcom.com (Carl Barron)
Date: Mon, 2 Apr 2007 05:56:52 GMT
Raw View
<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                      ]





Author: "jam" <farid.mehrabi@gmail.com>
Date: Mon, 2 Apr 2007 10:16:22 CST
Raw View
On Apr 1, 8:23 pm, chikito.chik...@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.
>
> -------------------------
> 2. This question is regarding strncpy library function.
>
> //function prototype of strncpy follows
> char *strncpy(char *s1, const char *s2, size_t n);
> We need to pad '\0' to the end of s1 when n is less than the length of
> s2 plus one.
>
> Compare strncpy with strncat. The function strncat pad '\0' by itself.
> Why this was left to the programmer in the case of strncpy function.
>
> ---

For the same reason that assignment operator is not void return(for
the sake of chaining):

int a,b,c,d;
a=b=c=d;
a+=b+=c+=d;
char * s1,s2,s3,s4;
strcpy(s1,strcpy(s2,strcpy(s3,s4)));
strcat (s1,strcat(s2,strcat(s3,s4)));


---
[ 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                      ]