String Manipulation Functions - strcpy, strncpy

  • Thread starter chikito.chikito
  • Start date
C

chikito.chikito

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.
 
P

Phlip

chikito.chikito said:
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 .

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 + ": ";
 
C

chikito.chikito

chikito.chikito said:
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 .

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 + ": ";


Philip,

I understand the reasons now. Thanks.
 
J

Jim Langston

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.
 
C

chikito.chikito

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.
//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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,128
Latest member
ElwoodPhil
Top