Before coming to this question, let me give some C statements:
int a, b, c;
a = b = c = 9;//= is right associative
//and returns the right value and hence
//c is assigned 9 followed by b and then a.
//Now, I do
c = 1;
c = b;
So, nothing abnormal here rt? But what if I do
1 = c;
Most people should catch the problem here. It should be an error. And this error is called "lvalue required" error. If we see the above 2 statements, "=" allows both constants and variables/expressions as RHS. But its LHS must always be a variable. The term variable is important here- it denotes that the content of that particular memory location can be modified. So, whwnever left side of "=" is not a variable we get an "lvalue required" error. Now lets come to the given question.
- int a = sizeof 3;
- *(1000) = 5;
- int a = 5; ((int)a)++;
- int b = 5, *a = &b; ((int*)a)++;
Here, 1 is fine. sizeof is an operator and not a function in C. We do not need to give a () unless we are using a type and not an expression.
All 2, 3 and 4 are errors.
- For 2, * operation requires that its operand must be a pointer but here it is a constant. Even normal variables are not allowed here. i.e.,
int a;
*a = 5;//invalid
int *p = &a;
*p = 5;//valid
*((int*)1000)= 5;
//syntactically correct but works only if 1000 is a valid memory location
-
int a = 5; ((int)a)++;
This will be more confusing. Without (int) it is fine. But what happens with (int) - typecasting is that it always returns the value. i.e., (int)a - returns the integer value of memory location a. This is fine if we use it as RHS of "=" as both "a=b" and "a=5" does the same operation if b is having value 5. But not when we use at LHS. This is same as 5++ which is not valid. Like "=", ++ also requires a variable - as it takes the value from the memory location increments it and stores back.
-
int b = 5, *a = &b; ((int*)a)++;
This is same as above- even for pointers type cast operator returns an rvalue- the value of the pointer. And this cannot be used for ++ or =. But we can do
int b = 5, *a = &b; (*a)++;//increments b
int b = 5, *a = &b; ((*)a)++;//syntax eror