C Arraygröße gegeben durch Variable
Ich habe heute einen Code gefunden, der mich verwirrt hat. Es hat so etwas gemacht:
#include <stdio.h>
int main(int argc, char **argv) {
int x = 5;
int foo[x];
foo[0] = 33;
printf("%d\n", foo[0]);
return 0;
}
Meine Frage ist, warum das funktioniert?
The arrayfoo
ist auf dem Stack, wie kann es also um @ erweitert werdex
?
Ich hätte so etwas erwartet:
#include <stdio.h>
int main(int argc, char **argv) {
int x = 5;
int foo[] = malloc(sizeof(int)*x);
foo[0] = 33;
printf("%d\n", foo[0]);
free(foo);
return 0;
}
Nicht, dass es schöner ist oder so, aber ich frage mich nur.