You can do this in C99 (or later), but your compiler may be set to C89/C90/ANSI-C by default. Try passing -std=c99
on the command-line.
If your compiler doesn't support C99-mode you'll need to use dynamic allocation via malloc
:
int * list = malloc(target * sizeof *list);
You should check if list
isn't NULL
, in case the malloc
failed and don't forget to call free(list)
when you're done with it!
Be aware that you've defined list
as an int
, and you're reading it with scanf("%d")
so it can be a negative value. That can lead to some undesirable behaviour. So consider changing the declaration to size_t
(the type that malloc
and VLA's expect). Then read it with %zu
(or %u
if %zu
isn't available) instead of %d
.
Finally, you should check the return-value of scanf
to make sure your value was read correctly, if it's less than the number of values you want to read (1 in your case) it will mean that value was not read and the contents of your target
remain unchanged. In your case target
will have undefined contents and so your program would invoke undefined behaviour if the user doesn't enter a valid number.