By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,808 Members | 1,612 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,808 IT Pros & Developers. It's quick & easy.

String Manipulation Functions - strcpy, strncpy

P: n/a
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.

Apr 1 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
chikito.chikito wrote:
1. Can someone tell me the difference between these two functions:
Firstly, you have a primarily C question, and this newsgroup works best for
C++.

C++ inherits the C Standard Library, including these functions, so everyone
here should know them. You need to learn C++ too, and only use high-level
systems like std::string in your programs. And if you have a reason to learn
and use C, you should send future questions like these to news:comp.lang.c .

Next, these questions are worded like homework. I will answer one in
grueling detail that might still get you in trouble, and snip the second
one. Read your tutorial - it's almost as erudite as me.
void strcpy(char *s1, const char *s2)
In general, never rename a library function and give it a different return
value or different contents. Your platform might rely on some detail of the
original function, and your new function might replace it. If you really
need an alternate string copy (doubtful), then you should give it a
different name, so nobody gets confused.

(And the C Standard reserves all identifiers beginning with str[a-z], so
follow that rule too.)
//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?
As a convenience to chain calls, such as:

strcat(strcpy(slug, label), ": ");

And note the equivalent in C++ is much clearer:

slug = label + ": ";

--
Phlip
http://flea.sourceforge.net/PiglegToo_1.html
Apr 1 '07 #2

P: n/a
On Apr 1, 10:33 pm, "Phlip" <phlip...@yahoo.comwrote:
chikito.chikito wrote:
1. Can someone tell me the difference between these two functions:

Firstly, you have a primarily C question, and this newsgroup works best for
C++.

C++ inherits the C Standard Library, including these functions, so everyone
here should know them. You need to learn C++ too, and only use high-level
systems like std::string in your programs. And if you have a reason to learn
and use C, you should send future questions like these to news:comp.lang.c .

Next, these questions are worded like homework. I will answer one in
grueling detail that might still get you in trouble, and snip the second
one. Read your tutorial - it's almost as erudite as me.
void strcpy(char *s1, const char *s2)

In general, never rename a library function and give it a different return
value or different contents. Your platform might rely on some detail of the
original function, and your new function might replace it. If you really
need an alternate string copy (doubtful), then you should give it a
different name, so nobody gets confused.

(And the C Standard reserves all identifiers beginning with str[a-z], so
follow that rule too.)
//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?

As a convenience to chain calls, such as:

strcat(strcpy(slug, label), ": ");

And note the equivalent in C++ is much clearer:

slug = label + ": ";

--
Phlip
http://flea.sourceforge.net/PiglegToo_1.html

Philip,

I understand the reasons now. Thanks.

Apr 1 '07 #3

P: n/a

<ch*************@gmail.comwrote in message
news:11*********************@b75g2000hsg.googlegro ups.com...
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.
You can use the returned pointer if you need to. Consider something like:

printf( "%s", strcat( str1, str2 ) );
or other fuctions that accept a string.
-------------------------
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.
strncpy basically does the exact same thing memcpy does. It copies n bytes
starting at sr2 into sr1. Those bytes can be binary, which can include a
null terminator (\0). Also, you may want to copy into the middle of a
string and appending an \0 would mess it up.

char Text[100];
strcpy( Text, "The blue pen is here." );
char Color[100];
strcpy( Color, "red " );
strncpy( Text + 3, Color );

This would replace "blue" with "red "
If strncpy added a null terminator, if you printed it would output:
"The red "
because of the null terminator.

With strcat, however, you are adding a string to the end of another string,
so there will be text at the end of the first string anyway.
Apr 1 '07 #4

P: n/a
On Apr 1, 10:59 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
<chikito.chik...@gmail.comwrote in message

news:11*********************@b75g2000hsg.googlegro ups.com...
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.

You can use the returned pointer if you need to. Consider something like:

printf( "%s", strcat( str1, str2 ) );
or other fuctions that accept a string.
-------------------------
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.

strncpy basically does the exact same thing memcpy does. It copies n bytes
starting at sr2 into sr1. Those bytes can be binary, which can include a
null terminator (\0). Also, you may want to copy into the middle of a
string and appending an \0 would mess it up.

char Text[100];
strcpy( Text, "The blue pen is here." );
char Color[100];
strcpy( Color, "red " );
strncpy( Text + 3, Color );

This would replace "blue" with "red "
If strncpy added a null terminator, if you printed it would output:
"The red "
because of the null terminator.

With strcat, however, you are adding a string to the end of another string,
so there will be text at the end of the first string anyway.

..... we may want to copy into the middle of a
string and appending an \0 would mess it up. So, it is better that the
programmer appends the \0 wherever necessary.

Thanks Langston

Apr 1 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.