Search code examples
carraysstructurelvalue

lvalue : array and structure


An lvalue is defined as an expression to which a value can be assigned. And it is illegal to assign and array with a array value. E.g.:

int x[2],y[2];
x = y;

Whereas structures can be treated as lvalues. Below structure assignment is valid.

typedef struct car {
    char color[20];
    int price;
} CAR;

CAR audi, bmw;
audi = bmw;

What is the difference?


Solution

  • There are historic reasons why arrays are not assignable on themselves, but are assignable inside structs. There is really no technical reason for this discrepancy.

    Anecdottal heresay is that when C was designed, it was based on a certain language (don't remember which one!) which didn't have array assingment, so this feature was exluded from C as well - to preserve compatibility. However, this language didn't have structs, so array assingment inside structs was OKayed.