0
typedef struct {
  int p[10];
} array10;

int main() {
  array10 a = {(int[10]){...}};
  array10 b = {(int[10]){...}};
  a.p = b.p; // #1. doesn't compile, can't assign to int[10]
  a = b; // #2. successfully assigns b.p to a.p
}

Although I understand what's happening here, I found it curious: If #2 works, then why isn't #1 in the language? You can assign to static arrays (and thus pass them by value) as long as they're wrapped in a struct. Am I missing something about the philosophy of the C that makes this intuitive?

bockyboh
  • 11
  • 1
  • 2
  • 1
    History. The name of an array decays to a pointer in all rvalue contexts, so as to allow indexing of arrays with simple pointer arithmetic (something C inherits from B, which didn't have structs) – Chris Dodd Jan 12 '23 at 22:41
  • Some useful info at this question: [What does impossibility to return arrays actually mean in C?](https://stackoverflow.com/questions/50808782) – Steve Summit Jan 12 '23 at 22:56
  • 1
    I do not see any static arrays here – 0___________ Jan 12 '23 at 23:01
  • @0___________: “Static” is a general English word for unchanging. The C standard uses it for “static storage duration” but also for other purposes, such as the `static` keyword inside subscripts of an array parameter declarator. – Eric Postpischil Jan 12 '23 at 23:10
  • In C language it has a very precise meaning. I do not see any "static" (stalic storage duratiuon) arrays. – 0___________ Jan 12 '23 at 23:28
  • @0___________: In the C standard, “static storage duration” has a very precise meaning. “Static” by itself does not; it is not a particularly defined term, and it has multiple uses. Some participants in Stack Overflow refer to objects as “static” to mean “static storage duration,” but this is not a precise use of language. – Eric Postpischil Jan 13 '23 at 00:54
  • @EricPostpischil in real life people use short forms. "static array" has quite precise meaning in the programmer's slang, the same as "static pipe" in the pilot community (and it does not mean "pipe which is not moving"). It is not artificially precise, indeed – 0___________ Jan 13 '23 at 01:06
  • @0___________: “Precise meaning” and “slang” conflict. OP used a word that is correct in regard to its English meaning and not in conflict with the C standard. Your stance on the use of language is baseless; you have been informed of what OP meant, and your claims on the language are no greater than anybody else‘s. – Eric Postpischil Jan 13 '23 at 01:20
  • @EricPostpischil no, because in slang something has a precise meaning. when in Rome do as the Romans do – 0___________ Jan 13 '23 at 01:44
  • @0___________: The meaning of “slang” is informal speech. It is, by definition, not precise. You may be confusing it with “jargon,” which is specialized terminology associated with a profession or activity.” – Eric Postpischil Jan 13 '23 at 02:10

1 Answers1

3

C standard says in 6.5.16p2

An assignment operator shall have a modifiable lvalue as its left operand.

And 6.3.2.1p1

... A modifiable lvalue is an lvalue that does not have array type ...

So arrays cannot be left-operands of assignment operator.

The rationale is the following. Whenever a value of an array is used then this value is transformed to a pointer to the array's first element (at index 0). Thus it is not possible to have an r-value expression which has array type. As result it is not possible to form anything that could be assigned to an array variable.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
tstanisl
  • 13,520
  • 2
  • 25
  • 40
  • I understand this - I was interested in why the language is this way. I always assumed it was an issue of compiler capability, until my question occurred to me. – bockyboh Jan 13 '23 at 12:45