1

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.

5
  • Constant expressions are allowed in array initializers. Commented Jan 2, 2012 at 19:05
  • Is your array static or it is being defined inside a function? In the latter case non-constant initializers are allowed in C99, here is an example from GCC docs. Commented Jan 2, 2012 at 19:08
  • 1
    @p.kolya, any expressions are allowed in function scope. Commented Jan 2, 2012 at 19:10
  • It's a pointer in a struct that gets malloc'd in the __arrayInit function. Commented Jan 2, 2012 at 19:20
  • 1
    Don't expect speed. For every element, it has to evaluate the expression and then store it. That's so no matter how it's done. Commented Jan 3, 2012 at 2:17

1 Answer 1

4

C99 allows expressions in initializer

int sarray[] = {
   [0] = x + y * z, // Sample Expression
   [1] = a / b + c, // Another
   [2] = 5, // sometimes there's a constant
};

is valid syntax.

Edit: If it is an auto variable any expression that has a type that is assignment compatible to the base type of the array is allowed.

For arrays with static storage class (either global arrays or local arrays that are additionally declared with static) you can use constant expressions composed of:

  • any type of literals such as 23, 1E-45, "hello"
  • enumeration constants
  • address constants of global variables or functions

Not working are const qualified variables.

Sign up to request clarification or add additional context in comments.

5 Comments

Do those have to equate to a constant?
@SamWashburn: Only if the array is in global scope. If it is in function scope any expression that has a type that is assignment compatible to the base type of the array is allowed.
@JensGustedt in c99, if in function scope but with the static storage specifier, initializers also have to be constant expressions. In c89, aggreate or union types initializers always have to be constant expressions (even if the storage duration is automatic).
@ouah, right reformulated to refer to the storage class. For C89 also correct, but the question explicitly talked about C99.
s/function scope/block scope/ in my last comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.