1

char label[10] = "Me" works, and to change the value of label, I'd have to do something like:

char * temp = "Not Me";
strcpy(label,temp);

My question is why is this invalid?

char label[] = "Me";
label = "Not me";
Ed S.
  • 122,712
  • 22
  • 185
  • 265
Louis93
  • 3,843
  • 8
  • 48
  • 94
  • 2
    Arrays are second-class citizens in C: you cannot assign arrays, you cannot pass arrays to a function... – ouah May 09 '13 at 20:48
  • possible duplicate of [Array Assignment](http://stackoverflow.com/questions/5279082/array-assignment) –  May 09 '13 at 20:49
  • `str(n)cpy( label, "Not me" );` would do that in one statement still.. – VoidPointer May 09 '13 at 20:50
  • 1
    Oddly, though, you can assign variables of type `struct { char label[10]; }` because `struct` assignment simple means bytewise copying. – dmckee --- ex-moderator kitten May 09 '13 at 20:52
  • 1
    @ouah: If arrays are second-class entities, why do so many people say that arrays “decay” to pointers? Shouldn’t they say that arrays are promoted to pointers? Or are pointers tertiary entities? – Eric Postpischil May 09 '13 at 20:57
  • @EricPostpischil They shouldn't. I don't like this wording and prefer to use the standard terminology that says that arrays are *converted* to pointers. – ouah May 09 '13 at 21:05
  • 1
    @EricPostpischil maybe because something could be lost in the conversion, like the array size. – effeffe May 09 '13 at 21:15

3 Answers3

5

Because arrays are not assignable. You can create them, you can change their content, but the array itself cannot be modified to refer to a new chunk of memory.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • 1
    @0A0D: Not the same thing. You are simply making a copy of the struct, which includes an array. But yes, it is a workaround. – Ed S. May 09 '13 at 21:39
0

The reason label="Not Me" will not work, is because the type of label is a char *. You defined it as an array, and arrays and pointers are semantically equivalent. A pointer is an address, basically an int so it does not make sense to assign something like "Not Me" to something that has a type of pointer.

What you need to do is dereference the pointer and assign a value to the location that is pointed to. Since the type is char * you would need to dereference each location and assign a character to each location.

E.g.

label[0] = 'N';
label[1] = 'o';
...

Or use some for loop equivalent.

Porkbutts
  • 924
  • 7
  • 12
  • 1
    `label` is not a pointer, it's an array, and these two types are *not* equivalent. Furthermore, if `label` would be a pointer the assignment would be valid (though dangerous without a `const` qualifier), and this actually *makes* sense because the string literal decays from array to pointer. – effeffe May 09 '13 at 20:50
-2

In C, an array label is essentially the same as a pointer label, except that the array automatically gets some memory allocated behind it when defined.

Otherwise, array labels and pointer labels are treated as the same thing. So even if you declared label as an array, if you operate directly on label (without braces after it), you are treating it as a pointer (that happens to point to the first array element). Likewise, if you declare label as a pointer, but operate on label[x], you are operating on the xth data item past the place label points to (aka: *(label + x)).

Your first instinct may be that looks crazy unsafe. You'd be right. The language was designed back in the late 60's and early 70's when making compilers easy to implement on the tiny machines of the day was much more important than how error-prone the langauge was.

T.E.D.
  • 44,016
  • 10
  • 73
  • 134
  • 1
    Arrays and pointers are not, in any reasonable sense, "the same thing". Array expressions decay to pointers in most, but not all, contexts. Consider, for example, `sizeof label` given either `char label[10];` or `char *label = "123546789";`. And by "labels", I think you mean "names" or "identifiers". – Keith Thompson May 09 '13 at 21:28
  • *declared* should be *defined*. Indeed, defining an array and then declaring it somewhere else (for example in another file with an `extern` declaration) as a pointer is another example of how arrays and pointers are different. – effeffe May 09 '13 at 22:56