1

I'm trying to use global variables to store 3 values from a file. When I run my project this is the error I get:

error: variably modified ‘pkt’ at file scope
error: variably modified ‘num’ at file scope

Here is my code:

config.h

#ifndef READCONFIG_CONFIG_H
#define READCONFIG_CONFIG_H

#include <stdio.h>

#define PATH "src/transferConfig.txt"

extern unsigned long int TIMEOUT_PKT;
extern int START_BUFFER_SIZE;
extern float PROBLOSS;
int getConfig(){
    FILE *fp;
    int n;
    float p;
    unsigned long int t;

    if((fp = fopen(PATH,"rt")) != NULL) {
        fscanf(fp, "N=%d\n p=%f\n T=%ld\n", &n, &p, &t);
        fclose(fp);
        TIMEOUT_PKT = t;
        START_BUFFER_SIZE = n;
        PROBLOSS = p;
    }else{
        TIMEOUT_PKT = 3000000;
        START_BUFFER_SIZE = 15;
        PROBLOSS = 0;
    }
}

#endif //READCONFIG_CONFIG_H

window.h

typedef struct window{
    packet* pkt[START_BUFFER_SIZE]; 
    long num[START_BUFFER_SIZE];
}window

Error during the build

error: variably modified ‘pkt’ at file scope

error: variably modified ‘num’ at file scope

How can I fix this error?

Increasingly Idiotic
  • 5,700
  • 5
  • 35
  • 73
Jack Law
  • 11
  • 1
  • 3
    Possible duplicate of [variably modified array at file scope in C](https://stackoverflow.com/questions/13645936/variably-modified-array-at-file-scope-in-c) – Increasingly Idiotic May 24 '18 at 16:00
  • 1
    `packet* pkt[START_BUFFER_SIZE];` will be problematic for globally initiating the struct. You can only make assignments to this memberafter providing memory. – ryyker May 24 '18 at 16:00
  • 1
    Don't define functions not explicitly declared `inline` in headers. Doing so means that only one source file can include that header in a given program, which makes the header close to pointless since their primary purpose is to share information between source files. – Jonathan Leffler May 24 '18 at 16:32
  • 1
    The size of a structure must be a compile-time constant — unless you're using a (single) flexible array member, but a FMA is not written as you showed. Basically, you need to rethink your design. You're likely to need some dynamic memory allocation to handle your structure(s). – Jonathan Leffler May 24 '18 at 16:35

1 Answers1

0

START_BUFFER_SIZE is not known at the compile time, and it's not constant as you are able to assign a value to it. Variable size arrays require dynamic memory allocation. You need to know your array size in compile time to be able to use a static array.

basak
  • 1,909
  • 2
  • 11
  • 7