I’m doing a program transformation from a language that allows expressions in array initializers to C99, which doesn’t.
Currently, the way I’m handling this is to create an __arrayInit function and then generate a very large number of these inside:
array[0] = x + y * z; // Sample Expression
array[1] = a / b + c; // Another
array[2] = 5; // sometimes there's a constant
...
Often there are hundreds of these individual initializations. Is there a better way of doing this that executes faster? Kudos if it compiles faster as well.
Edit: The expressions are sometimes non-constant and can have variables and function calls in them.
C99 allows expressions in initializer
is valid syntax.
Edit:
If it is an
autovariable any expression that has a type that is assignment compatible to the base type of the array is allowed.For arrays with
staticstorage class (either global arrays or local arrays that are additionally declared withstatic) you can use constant expressions composed of:23,1E-45,"hello"Not working are
constqualified variables.