我想打一個隨機大小的數組每次程序執行在第一,但編譯罵我開頭的隨機數組大小?
"Error 2 error C2466: cannot allocate an array of constant size 0"
有什麼辦法,我可以隨意選擇SIZE
通過SIZE = rand() % 100
開頭,然後用int myarray[SIZE]={0}
intialize的陣列? ??或者我應該每次在開始時用一個確切的數字初始化它?
int main(void) {
int i;
int SIZE=rand()%100;
int array2[SIZE]={0};
for(i=0;i<SIZE;i++) //fill the array with random numbers
array2[i]=rand()%100;
...
}
當然,使用malloc。 – Mike 2013-05-02 18:00:35
您需要一個編譯器,該編譯器在一個標準中沒有被卡住,同時被替換兩次。在C99中添加了VLA(但不能初始化VLA,如'int array2 [SIZE] = {0}')。 – 2013-05-02 18:01:35
堆棧上變量的分配要求知道大小。如果您希望動態調整大小,請使用堆('malloc()')。 – 2013-05-02 18:02:56