You're trying to use a variable length array at global scope. This won't work (globals need to have a constant, known size, otherwise compilation would be difficult).
IMHO, you shouldn't be using a global in the first place. Better use a local variable, and pass it via argument to the functions / parts of your program that need access to it.
IMHO, you shouldn't be using VLA in the first place.
I'd go with something like this:
int main(int argc, char ** argv) {
// check arguments, not done here!
int value = atoi(argv[1]);
// Check that it's actually usable as a size!
size_t count;
if (value >= 0) {
count = value;
}
else {
// Fires of hell here
exit(1);
}
int * amount = malloc(sizeof(int) * count); // add error check, please!
// use at will
free(amount);
return 0;
}
If you insist on using a global, then there's the possibility to make the (constant sized) pointer amount
a global variable.
Also: Using heap allocated data instead of stack allocated if you'd use a VLA is to be preferred when accessing the data from a detached thread, because the VLA could already be out of scope when the thread tries to access it!
int* amount; int main(int argc, char** argv){ amount = malloc(atoi(argv[1]) * sizeof(int));
etc andfree
it at the end ofmain
. I leave error checking upto you. – Katrinka