22

I was curious about why this is not allowed in C:

char myarray[4];

myarray = "abc";

And this is allowed:

char myarray[4] = "abc";

I know that in the first case I should use strcpy:

char myarray[4];

strcpy(myarray, "abc");

But why declaration and later initialization is not allowed and declaration and simultaneous initialization is allowed? Does it relate to memory mapping of C programs?

Thanks!

5 Answers 5

41

That's because your first code snippet is not performing initialization, but assignment:

char myarray[4] = "abc";  // Initialization.

myarray = "abc";          // Assignment.

And arrays are not directly assignable in C.

The name myarray actually resolves to the address of its first element (&myarray[0]), which is not an lvalue, and as such cannot be the target of an assignment.

Sign up to request clarification or add additional context in comments.

2 Comments

&myarray[0] is not an lvalue? But I thought an lvalue was just a memory location. The problem in this case is myarray is const, not that it is not an lvalue.
@Dave, an lvalue is a "memory location", and that's why &myarray[0] is not an lvalue -- it's an address, not the memory location it points to.
5

Yes, this is a kind of inconsistency in the language.

The "=" in myarray = "abc"; is assignment (which won't work as the array is basically a kind of constant pointer), whereas in char myarray[4] = "abc"; it's an initialization of the array. There's no way for "late initialization".

You should just remember this rule.

4 Comments

There is no inconsistency at that point. marray is an array and not a pointer, that's all.
@Jens: the inconsistency is in the fact that the initialization is denoted syntactically the same way as the assignment. This is exactly what led to the misunderstanding.
That syntax there is only a shortcut. I think that char myarray[4] = { "abc" }; makes things much clearer and unambiguous.
@Jens: the syntax you mention in the last comment differs from the one which OP is discussing. This syntax would be of course less ambiguous. In my opinion, having initialization and assignment visually different should be a good hint to the developers that they are not the same. The syntax could be perhaps char myarray[4]("abc"); or something like that.
1

This is another C example of where the same syntax has different meanings (in different places). While one might be able to argue that the syntax should be different for these two cases, it is what it is. The idea is that not that it is "not allowed" but that the second thing means something different (it means "pointer assignment").

Comments

0

I think these are two really different cases. In the first case memory is allocated and initialized in compile-time. In the second - in runtime.

Comments

0
myarray = "abc";

...is the assignation of a pointer on "abc" to the pointer myarray.

This is NOT filling the myarray buffer with "abc".

If you want to fill the myarray buffer manually, without strcpy(), you can use:

myarray[0] = 'a', myarray[1] = 'b', myarray[2] = 'c', myarray[3] = 0;

or

char *ptr = myarray;
*ptr++ = 'a', *ptr++ = 'b', *ptr++ = 'c', *ptr = 0;

Your question is about the difference between a pointer and a buffer (an array). I hope you now understand how C addresses each kind.

3 Comments

"...is the assignation of a pointer on "abc" to the pointer myarray." myarray is an array, not a pointer.
You don't want to use signatures. You already have one below the answer.
@Raynos: they are very much alike, but not the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.