ARM Technical Support Knowledge Articles

Why do I see "Warning: #1296-D: extended constant initialiser used"?

Applies to: RealView Development Suite (RVDS)

Answer

When compiling C code containing statements like this:

int x;
int y = (int) &x;

where x and y are static objects (global variables or static local variables), the compiler will report:

Warning: #1296-D: extended constant initialiser used

This warning occurs because the above C statement is not valid ANSI C code (even though other compilers may accept it). The C standard does not permit a cast of 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.

Chapter 6.6 of the C99 standard 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.  Unfortunately, this is a common ANSI non-compliancy amongst other compilers, and can result in problems when porting legacy code to ARM, which is why the ARM compiler only issues a warning rather than an error message.

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 incorrect example is:

char value;
long array[] = {
(long)&value,
(long)"string"
};

The above code could be rewritten as:

char value;
char *array[] = {
(char*)&value,
(char*)"string"
};

(Pointer to pointer casts are allowed in C.)

Article last edited on: 2011-10-07 10:23:07

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential