D
dspfun
I'm trying to get a good understanding of how unary operators work and
have some questions about the following test snippets.
int *p;
~!&*++p--;
It doesn't compile, why? The problem seems to be the ++, the compiler
says: "Error: invalid l-value in increment".
int i = 10;
~!*&i++;
It doesn't compile, why? The problem seems to be the ++, the compiler
says: "Error: invalid l-value in unary "&"".
int *p;
~!-&*p--;
It doesn't compile, why? The problem seems to be the -, the compiler
says: "Error: invalid type of argument to unary minus".
I guess the problem is that you can't do unary minus on pointers, and
it doesn't make sense to do so either.
The following does compile on the other hand:
int *p;
p = (int *) = 0x10101010; /*Just to set it to some value..*/
~!&*p--;
p results in having the value 0x1010100c which I understand why.
Is it possible to make an expression with all the unary operators one
after another?
In general, is it good programming practice to make use of the
precedence and associativity of operators?
Using precedence/associativity of operators when combining the
operators (probably) reduces the number of codelines (compared to
"spelling it out"), but doesn't it make the source code more difficult
to read?
Isn't it better to instead "spell it out"?
Another question, when would you use the unary operator sizeof with
other unary operators? To save code lines?
Quite a few questions, hmm, hope someone will be able to respond..!
BR!
have some questions about the following test snippets.
int *p;
~!&*++p--;
It doesn't compile, why? The problem seems to be the ++, the compiler
says: "Error: invalid l-value in increment".
int i = 10;
~!*&i++;
It doesn't compile, why? The problem seems to be the ++, the compiler
says: "Error: invalid l-value in unary "&"".
int *p;
~!-&*p--;
It doesn't compile, why? The problem seems to be the -, the compiler
says: "Error: invalid type of argument to unary minus".
I guess the problem is that you can't do unary minus on pointers, and
it doesn't make sense to do so either.
The following does compile on the other hand:
int *p;
p = (int *) = 0x10101010; /*Just to set it to some value..*/
~!&*p--;
p results in having the value 0x1010100c which I understand why.
Is it possible to make an expression with all the unary operators one
after another?
In general, is it good programming practice to make use of the
precedence and associativity of operators?
Using precedence/associativity of operators when combining the
operators (probably) reduces the number of codelines (compared to
"spelling it out"), but doesn't it make the source code more difficult
to read?
Isn't it better to instead "spell it out"?
Another question, when would you use the unary operator sizeof with
other unary operators? To save code lines?
Quite a few questions, hmm, hope someone will be able to respond..!
BR!