Can you explain how the sizeof()
works with a random length array? I thought sizeof()
on an array is calculated during the compilation, however, the size of an array with random length seems to be calculated correctly.
Example:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(){
srand ( (unsigned)time ( NULL ) );
int r = rand()%10;
int arr[r]; //array with random length
printf("r = %d size = %d\n",r, sizeof(arr)); //print the random number, array size
return 0;
}
The output from multiple runs:
r = 8 size = 32
r = 6 size = 24
r = 1 size = 4
Compiler: gcc 4.4.3