I recently saw a code and it declared an array using function parameter as array's size. I don't know how, but this code compiles and works properly (using gdb online). Do you know what compiles do in this case?
Code used:
void foo(int size)
{
size++;
char arr[size];
for (int i = 0; i < size; i++) {
arr[i] = i;
printf("arr[%d] = %d\r\n", i, arr[i]);
}
}
int main()
{
printf("Hello World\r\n");
foo(10);
return 0;
}
The output of this code is:
Hello World
arr[0] = 0
arr[1] = 1
arr[2] = 2
arr[3] = 3
arr[4] = 4
arr[5] = 5
arr[6] = 6
arr[7] = 7
arr[8] = 8
arr[9] = 9
arr[10] = 10
...Program finished with exit code 0
Press ENTER to exit console.