I'm reading up on and learning about pointers in C and have read that, if you want to create an array that you'll only know the length of at runtime, the correct way to do this is using malloc:
so that you end up with a pointer to the start of the array of length i.
However, when mucking about and testing this out I found that the compiler (GCC) doesn't throw up any complaints when you use:
Is this just a fluke of how GCC compiles it that it takes into account the dynamic memory allocation for you? Would other compilers throw up an error? Or is the second method actually allowed?
Code:
int *iptr;
iptr = malloc(i * sizeof(int)); // i to be read in at runtime
so that you end up with a pointer to the start of the array of length i.
However, when mucking about and testing this out I found that the compiler (GCC) doesn't throw up any complaints when you use:
Code:
int i;
scanf("%d", &i);
int test[i];
Is this just a fluke of how GCC compiles it that it takes into account the dynamic memory allocation for you? Would other compilers throw up an error? Or is the second method actually allowed?