Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C - why is strcpy() necessary

Tags:

c

string

strcpy

Can someone please explain to me why strcpy() is necessary to assign strings to character arrays, such as in the following code snippet.

int main(void) {  char s[4];  s = "abc"; //Fails strcpy(s, "abc"); //Succeeds  return 0; } 

What is the reason that s = "abc" fails? And why is strcpy() the only way to assign strings to char arrays after they have been declared? It seems strange to me that you have to use a function to carry out a basic assignment.

like image 544
C_p678 Avatar asked Aug 01 '11 15:08

C_p678


People also ask

Should I use strcpy?

Using strcpy() function to copy a large character array into a smaller one is dangerous, but if the string will fit, then it will not be worth the risk. If the destination string is not large enough to store the source string then the behavior of strcpy() is unspecified or undefined.

Why do we use string copy?

strncpy is used to copy first several characters from source string to destination string. It doesn't append any NULL character when the copying finishes.

Why we use strcpy in structure?

The strcpy() function is used to copy strings. It copies string pointed to by source into the destination . This function accepts two arguments of type pointer to char or array of characters and returns a pointer to the first string i.e destination .

What can be used instead of strcpy?

The strncpy() library function performs a similar function to strcpy() but allows a maximum size n to be specified: 1 char *strncpy( 2 char * restrict s1, const char * restrict s2, size_t n 3 );


2 Answers

Arrays in C are non-assignable and non-copy-initializable. That's just how arrays are in C. Historically, in value context (on the RHS of assignment) arrays decay to pointers, which is what formally prevents assignment and copy-initialization. This applies to all arrays, not only to char arrays.

C language inherits this arrays behavior from its predecessors - B and BCPL languages. In those languages arrays were represented by physical pointers. (And obviously re-assignment of pointers is not what you'd want to happen when you assign one array to another.) In C language arrays are not pointers, yet they do "simulate" the historical behavior of B and BCPL arrays by decaying to pointers in most cases. This historical legacy is what keeps C arrays non-copyable to this day.

One exception from the above is the initialization with a string literal. I.e. you can do

char c[] = "abc"; 

in which case conceptually we are copying string literal "abc" to array c. Another exception is array wrapped into a struct type, which is copied when the whole struct object is copied. And that's about it.

This means that whenever you want to copy a naked (non-wrapped) array, you have to use a library-level memory copying function, like memcpy. strcpy is just a flavor of that specifically tailored to work with strings.

like image 177
AnT Avatar answered Oct 08 '22 19:10

AnT


That's simply what arrays are in C. You can't assign to them. You can use pointers if you like:

char *p; p = "abc"; 

Incidentally, there is a C FAQ.

Arrays are ``second-class citizens'' in C; one upshot of this prejudice is that you cannot assign to them.

like image 21
cnicutar Avatar answered Oct 08 '22 19:10

cnicutar