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.
Up vote 1 down vote favorite 1 share g+ share fb share tw.
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 s4; 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. C string strcpy link|improve this question asked Aug 1 '11 at 15:58C_p678261.
3 It was designed this way to encourage you to move on to a more humane language with real strings. – David Heffernan Aug 1 '11 at 16:02.
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. One exception from this rule is the initialization with a string literal. I.e.
You can do char c = "abc"; but that's it. This means that whenever you want to copy an 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.
Just to clarify, all array types can be initialized with a proper initializer of the form { val0, val1, ... }. – Jens Gustedt Aug 1 '11 at 21:29.
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.
Yeah I do use pointers, I just don't understand why s = "abc" doesn't work in my example. S is a char array, and so is "abc"... – C_p678 Aug 1 '11 at 16:03 3 @C_p678 - no, s is a char array, "abc" is a pointer to constant string. – Binyamin Sharet Aug 1 '11 at 16:03 1 @MByD: Not entirely correct.
"abc" is not a pointer. "abc" an array of type char4, which in this context decays to a pointer of type char *. Note, that in C the string is not constant.
It is non-modifiable all right, yet the type itself does not include const qualifier. – AndreyT Aug 1 '11 at 16:09 @AndryT: To be even pickier, "const" and "constant" are two very different things. "const" probably should have been called "readonly".
A constant, or a constant expression, is one that can be evaluted at compile time; a const object is one that cannot be modified at runtime. Consider const int r = rand();. – Keith Thompson Aug 2 '11 at 3:03.
Short answer: historical reasons. C never had a built in string type. It wasn't until C++ came along that std::string came into being, and even that did not arrive with the first implementations Long answer: the type of "abc" is not char, but rather char *.
Strcpy is one mechanism with which you can copy the data that the pointer points at (in this case ABC). Strcpy isn't the only way to initialize an array, however, it is smart enough to detect and respect the terminating 0 at the end of the string. You could also use memcpy to copy the string into s but that requires you pass in the length of the data to be copied, and to ensure the terminating 0 (NULL) is present in s.
The type of "abc" is char4. – Jens Gustedt Aug 1 '11 at 21:27 And strcpy isn't an initialization but an assignment. Character arrays can be initialized as all other arrays, see AndreyT's answer.
– Jens Gustedt Aug 1 '11 at 21:31.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.