问题描述:

I need to creates an array of SIZE integers, fill the array with random numbers, then display the unsorted and sorted array contents. Here's what I got so far:

#include <iostream>

#include <stdio.h>

#include <stdlib.h>

using namespace std;

int rand(void) ;

int printArray(int array[], int size)

{

return 0 ;

}

int sortArray(int array[], int size)

{

return 0 ;

}

int main(int argc, char *argv[])

{

int size = atoi(argv[1]) ;

int* array = new int [size] ;

for (int i = 0 ; i < size; i++)

{

int array[i] = rand() % 100 ;

}

//printArray(argv[1], size) ;

//sortArray(argv[1], size) ;

return 0;

}

The error I'm getting:

||In function 'int main(int, char**)':|

|26|error: variable-sized object 'array' may not be initialized|

|26|warning: unused variable 'array'|

|22|warning: unused variable 'array'|

||=== Build finished: 1 errors, 2 warnings ===|

I assume it's because the array has to be filled at run time?

网友答案:
int array[i] = rand() % 100 ;

Remove int from this line. The compiler thinks you are (re)declaring an array of size i which happens to be variable. It should be

array[i] = rand() % 100 ;

Since you are not doing anything with the declared array in the loop, you get an unused variable warning.

相关阅读:
Top