C always copies an element when reading from an array with A[i] , that is, when the expression A[i] is "rvalue". However, when considering a notation, C has the concept of an βlvalueβ of expressions, which are essentially a limited subset of the syntax of expressions that can appear as a destination:
X = Y *X = Y X[i] = Y Xn = Y
In these cases, the βexpressionsβ *X , X[i] and Xn do not actually evaluate the values ββ- they have the same syntax as the expressions, for convenience, but not for the same semantics. You can think of it as something more like the following:
memcpy(&X, &Y, sizeof(Y)); memcpy(&*X, &Y, sizeof(Y)); memcpy(&X[i], &Y, sizeof(Y)); memcpy(&X.n, &Y, sizeof(Y));
Or, alternatively, think that C has several different assignment operators:
_ = _ // direct assignment *_ = _ // indirect assignment _[_] = _ // special case of indirect assignment _._ = _ // special case of indirect assignment
In any case, an assignment such as A[5] += 1 will increase the value of the sixth element of A in place, as you would expect, you can check like this:
int A[1] = { 1 }; A[0] += 5; printf("%d\n", A[0]);
Jon purdy
source share