0

I want to create a array with the size that is calculated in the preprocessor by means of defines. When I try to initialize this array "memory" the following error appears:

error: expression must be an integral constant expression

This look the me that the expression that gives the size appears to be variable, which is weird because it is calculated from defines. See the following code:

#define SAMPLE_FREQUENCY_HZ 48000
#define SAMPLE_INTERVAL_MS ((1.0 / SAMPLE_FREQUENCY_HZ) * 1000.0)
#define MAX_DELAYLINE_LENGHT_MS 250
#define MAX_DELAYLINE_LENGHT_ELEM (MAX_DELAYLINE_LENGHT_MS / SAMPLE_INTERVAL_MS)

typedef struct {
    float delay;
    int size;
//    float *memory;
    float memory[MAX_DELAYLINE_LENGHT_ELEM];
    float *start;
    float *end;
    float *writePointer;
    float *readPointer;
} delayline_t;

Could someone explain the problem en give me a solution the solve this in a need way. Thanks!

3 Answers 3

1

MAX_DELAYLINE_LENGHT_ELEM is a float/double but an array size must be integral.

It resolves to :

(250 / (1.0 / 48000*1000.0)) =12222.0

but to the compiler it is still a double.

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

Comments

0

The expression that determines the array size of a member in the struct, must be an integer constant expression.

6.6 Constant expressions

  1. An integer constant expression 117) shall have integer type and shall only have operands that are integer constants, enumeration constants, character constants, sizeof expressions whose results are integer constants, _Alignof expressions, and floating constants that are the immediate operands of casts. Cast operators in an integer constant expression shall only convert arithmetic types to integer types, except as part of an operand to the sizeof or _Alignof operator.

As you can see, floating point types are not allowed in this expression.

You will have to manually pre-compute the values of your macros, round them to integer values, and then include them into the program.

Comments

0

The array size needs to be an integer constant.

Suggest 3 changes: A) Change to nanoseconds. B) use rounded integer calculations 2) "LENGTH" (spelling)

#define SAMPLE_FREQUENCY_HZ (48000 /* Hertz */ )

#define SAMPLE_INTERVAL_ns \
    ((1000UL*1000*1000 + SAMPLE_FREQUENCY_HZ/2)/ SAMPLE_FREQUENCY_HZ)

#define MAX_DELAYLINE_LENGTH_ns (250UL*1000*1000 /* nano seconds */)

#define MAX_DELAYLINE_LENGTH_ELEM \
    ((MAX_DELAYLINE_LENGTH_ns + SAMPLE_INTERVAL_ns/2)/ SAMPLE_INTERVAL_ns)

Comments

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.