-4

The size of the array is defined as a symbolic constant(macro) as given below:

#define N (int)(N_AB+0.5*(LxIni+LxIni))

N is used later to declare global arrays consisting of double elements. The global arrays are declared as follows:

double rx[N];

However, I get the following error:

variably modified ‘rx’ at file scope

could you please help me with this?

Falcons
  • 21
  • 2
  • 5
    Please post a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – MikeCAT Mar 29 '21 at 12:49
  • Please post what is `N_AB` and `LxIni`. What is the point of `0.5*(LxIni+LxIni)`? `N is used later to declare global array’s consisting of double elements` Please instead of explaining , _show_ the code. How is `N` used _exactly in code_? `int rx[N][N]`? – KamilCuk Mar 29 '21 at 12:50
  • Likely duplicate (among many...): [**array bound is not an integer constant before '\]' token when it is actually constant**](https://stackoverflow.com/questions/60694542/array-bound-is-not-an-integer-constant-before-token-when-it-is-actually-cons) – Andrew Henle Mar 29 '21 at 12:52
  • N_AB and LxIni are also symbolic constants to compute N. – Falcons Mar 29 '21 at 12:53
  • 3
    `N_AB and LxIni are also symbolic constants to compute N` What is a "symbolic constant"? Please do not explain the code, please _show_ the code instead. Please `Edit` your post and post additional helpful information there. In C language there is no "symbolic constant"s - an identifier introduced with `#define` is called a "macro". – KamilCuk Mar 29 '21 at 12:57
  • @Falcons Welcome to StackOverflow. Please include some minimal code – Omid Mar 29 '21 at 12:58
  • Why do you calculate half the sum of `LxIni` and `LxIni`? It is “harmless” but hardly necessary. – Jonathan Leffler Mar 29 '21 at 12:59

2 Answers2

0

variably modified ‘rx’ at file scope

This error message means that you may not define a variable length array with static storage duration (file scope variables have static storage duration). You need to allocate the array dynamically or declare it in a block scope. In the last case without the storage specifier static it will have automatic storage duration.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

The floating-point 0.5 prevents the array size from being considered an integer constant expression. You can divide by the integer 2 instead, changing 0.5*(LxIni+LxIni) to (LxIni+LxIni)/2. You could also change it to LxIni, which should be mathematically equivalent if there is no overflow or wrapping. (Is that a typo, did you mean to add two different things rather than adding something to itself and then dividing by two, which seems pointless?)

This is intentional in the design of C; the size of a static-duration array must be an integer constant expression, which cannot use floating-point operands in arithmetic (except that a C implementation could choose to allow them).

C 2018 6.7.6.2 2 says objects with static storage duration (which includes arrays defined outside functions) must not have a variable length array type:

… If an identifier is declared to be an object with static or thread storage duration, it shall not have a variable length array type.

6.7.6.2 4 says if the size is not an integer constant expression, the array is a variable length array type:

… If the size is an integer constant expression and the element type has a known constant size, the array type is not a variable length array type; otherwise, the array type is a variable length array type…

6.6 6 restricts the use of floating-point constants in integer constant expressions [bold added]:

… An integer constant expression 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

A C implementation could choose to support floating-point arithmetic in integer constant expressions, as 6.6 10 says:

An implementation may accept other forms of constant expressions.

However, your implementation apparently choose not to do so.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312