c - What are the differences between constant expressions and nonmodifiable lvalues? -
from c in nutshell:
constant expressions
the compiler recognizes constant expressions in source code , replaces them values. resulting constant value must representable in expression’s type. may use constant expression wherever simple constant permitted.
operators in constant expressions subject same rules in other expressions. because constant expressions evaluated @ translation time, though, cannot contain function calls or operations modify variables, such assignments.
what constant expressions? doesn't define constant expressions
what differences between constant expressions , non-modifiable lvalues (e.g. array names, lvalues have been declared
const
)are constant expressions non-lvalues?
are non-modifiable lvalues constant expressions?
what constant expressions?
§6.6- constant expression:
a constant expression can evaluated during translation rather runtime, , accordingly may used in place constant may be.
one of constraint on constant expression
constant expressions shall not contain assignment, increment, decrement, function-call, or comma operators, except when contained within subexpression not evaluated115).
what differences between constant expressions , nonmodifiable lvalues?
non modifiable lvalues not constant expressions. non-modifiable values lvalues can't used modify object.
int const = 5;
i
referring const object lvalue, special kind of lvalue called non-modifiable lvalue.
wherever standard says "lvalue", means "modifiable lvalue" (you won't find in standard sake of clarity)
now let me explain bit more.
int x = 5; int const *p;
x
non const object , modifiable lvalue. p
pointer const int
object.
p = &x; // raise warning though , hazardous do.
above assignment uses qualification conversion convert value of type pointer int
value of type pointer const int
.
*p
, x
2 different expression referring same object. abject can modified using x
--x;
but can't done using *p
non-modifiable lvalues.
--(*p); // error
one more difference between constant expression , non-modifiable lvalue
int const x = 5; int *p; /* assigning address of non-modifiable object */ p = &x // correct /* assigning address of constant expression */ p = &5 // wrong
are constant expressions non-lvalues?
yes, constant expressions non-lvalues, i.e rvalues.
are nonmodifiable lvalues constant expressions?
no.
Comments
Post a Comment