Description When compiling C code containing statements like this:
int x; int y = (int) &x;
where x and y are static objects (e.g. global variables or static local variables), the compiler will report (for SDT 2.50 and earlier)
Serious error: illegal in static integral type initialiser: 'unary &'
or (for SDT 2.51 and ADS)
Warning: extended constant initialiser used
This is not valid ANSI C code (even though other compilers may accept it) because it is not allowed to cast a pointer to an integer in a constant expression. ANSI C requires the initialiser for a static object to be a constant expression, '(int) &x' is not considered to be a constant expression. Paragraph 3.4 of the ANSI spec says: "An arithmetic constant expression shall have arithmetic type and shall only have operands that are integer constants, floating constants, enumeration constants, character constants, and sizeof expressions. Cast operators in an arithmetic constant expression shall only convert arithmetic types to arithmetic types, except as part of an operand to the sizeof operator." Addresses are not arithmetic types, so the code above is disallowed for ANSI C. However, the above code is valid pcc C, so compiling with '-pcc' will work (but remember there are several other differences between -ansi and -pcc that may affect your code, such as signed characters). The code is also valid for C++. Unfortunately, this is a common ANSI non-compliancy amongst other compilers, and can result in problems when porting legacy code to ARM. To help with porting, the 'Serious error' has been downgraded to a 'Warning' (unless -strict is used) for SDT 2.51 and ADS. Solution The best solution is to correct the code to something like:
int x; int* y = &x;
so that y is now a pointer to x. Another example
char value; long array[] = { (long)&value, (long)"string" };
should be rewritten as:
char value; char *array[] = { (char*)&value, (char*)"string" };
(pointer to pointer casts are allowed in C).
|