In the following code a
array is initialized with s
"seed" variable, which is clearly not constant expression (as it's evaluated "run-time"):
#include <stdio.h>
int main(void)
{
int s = 1, i;
int a[] = {s, s + 2, s + 4, s + 6, s + 8};
for (i = 0; i < (int) (sizeof a / sizeof a[0]); i++)
printf("%d ", a[i]); /* prints: 1 3 5 7 9 */
putchar('\n');
return 0;
}
It compiles in gcc -Wall -Wextra
with no warnings. However adding -pedantic
provokes:
check.c: In function ‘main’:
check.c:8: warning: initializer element is not computable at load time
check.c:8: warning: initializer element is not computable at load time
check.c:8: warning: initializer element is not computable at load time
check.c:8: warning: initializer element is not computable at load time
check.c:8: warning: initializer element is not computable at load time
Does C require constant expression for initializer elements ?