c++ - 从 int 到 int 的无效转换*

标签 c++ arrays function loops int

我收到错误:

从 int 到 int* 的转换无效。

我没有创建任何 int*(我认为),当我将有问题的行更改为 int* 时,没有构建错误,但程序在启动时崩溃。

这是我的代码:

//Main:
int main(){

    //Varibales:
    Random randomInt;
    clock_t start;
    clock_t End;
    double duration;
    double clocksPerSec;
    int result;
    int arraySize;


    //Repeat 100 times:
    for(int i=1; i<=100; i++){

        //Set array size:
        arraySize = i*500;
        //Create the array:
        int testArray[arraySize];

        //For the array size:
        for(int j=0; j<arraySize; j++){


            //Add random number to array:
            testArray[j] = randomInt.randomInteger(1, 10000);

        }

        //Run the test:
        start = clock();
        result = algorithmTest(testArray[arraySize], arraySize);
        End = clock();

        //Calculate execution time:
        duration = End - start;
        clocksPerSec = duration/CLOCKS_PER_SEC;

        //Display the result:
        cout << "The median is: ";
        cout << result << endl;
        cout << "Exection time was: ";
        cout << clocksPerSec;
        cout << "s\n" << endl;

    }

    //Return 0:
    return 0;

}

当我调用 algorithmTest() 时似乎会抛出错误;在这里:

//First Test:
int algorithmTest(int testArray[], int Size){

    //Declare variables:
    int k = Size/2;
    int numSmaller;
    int numEqual;

    //For every element in the array:
    for(int i=0; i<Size; i++){

        //Set varibales to 0:
        numSmaller = 0;
        numEqual = 0;

        //For every element in the array:
        for(int j=0; j<Size; j++){

            //If j is less than i:
            if(testArray[j] < testArray[i]){

                //Increment numSmaller:
                numSmaller++;

            //Else if j is equal to i:
            }else if(testArray[j] == testArray[i]){

                //Increment numEqual:
                numEqual++;

            }
        }

        //Determine if the median was found:
        if(numSmaller < k && k <= (numSmaller + numEqual)){

            //Retrun the medain:
            return testArray[i];

        }
    }

    //Return 0:
    return 0;

}

最佳答案

result = algorithmTest(testArray[arraySize], arraySize);

应该是

result = algorithmTest(testArray, arraySize);

您的函数 int algorithmTest(int testArray[], int Size)int[] 作为第一个参数,同时您传递 testArray[arraySize] ,其中 [i] 运算符表示获取 testArrayith<​​ 元素的值,这是一个 int。因此,您会遇到该错误。

为了澄清一些事情, int testArray[arraySize]; 行中的 [...] 不同于 [...] result = algorithmTest(testArray[arraySize], arraySize); 行中:第一个用于指示数组的大小,而第二个用于访问元素。

关于c++ - 从 int 到 int 的无效转换*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43305008/

相关文章:

c++ - 从 VS6 到 VS2010 的 ostringstream 转换

c++ - 为什么 PointerAlignment 选项不起作用?

javascript - 你能帮我理解 sort() 在 Javascript 中是如何工作的吗?

Java For 循环作为参数

c - 如果字符串在 c 中相同,则返回 0,代码中发生了什么?

c++ - Arduino/C++ 中的函数/方法定义错误。还是语法错误?

python - 在 OSX 上使用 openMP 支持编译 cython

c++ - clang-linux : reporting CFI errors without crashing. ftrap-函数和-O2

javascript - 读取文本文件行并将 x 和 y 值存储在数组中 (NodeJS)

c - 如何调用 char * arr[]