I have this code for example.
#include <stdlib.h>
#include <stdio.h>
#define array_size 3
typedef struct {
int array[array_size];
} TEST;
void printout(TEST *p, int element) {
printf("element: %i
", p->array[element]);
}
int main(void) {
TEST *p;
p = malloc(sizeof(TEST));
p->array[0] = 5;
printout(p, 0);
return 0;
}
But I'd like to assign "array_size"
based on user input.
If I try to do so, the compiler says "variably modified ‘array_size’ at file scope"
. So, am I right that the only way to do what I want is to move everything to main()..?
It works just fine, but keeping structs and functions declarations
in file scope seems, you know, neat.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…