arrayscgccansi-c

ANSI C -> Arrays -> Variably modified at file scope


While I'm trying to explore possibilities of arrays in C in ANSI, I'm confronted with an issue. Here's my code:

#include <stdio.h>
#include <string.h>

static int MAXLIGNE = 5000;
char *ptrlig[MAXLIGNE]; // Ptr sur la ligne de txt // Got an issue:
                        // Variably modified ptrlig @ filescope

int lirelignes(char *ptrlig[], int nlignes);
void ecrirelignes(char *ptrlig[], int nlignes);
void trirapide(char *ptrlig[], int gauche, int droite)

Error from the GCC:

VARIABLY MODIFIED PTRLIG at FILESCOPE

I've seen that 'const' type may create that kind of issues. I tried to make it like:

#include <stdio.h>
#include <string.h>

static int MAXLIGNE = 5000;
unsigned char *ptrlig[MAXLIGNE];

But that doesn't seem to change anything in this case.


Solution

  • The length of an array defined at file scope must be a compile time constant, and the value of another variable does not qualify as such.

    If you want to use a name for the length of this array, you'll need to use a macro:

    #define MAXLIGNE 5000
    char *ptrlig[MAXLIGNE]; 
    

    The macro does a direct text substitution, so after the preprocessor stage it is the same as char *ptrlig[5000];