繁体   English   中英

从int到int的无效转换*

[英]invalid conversion from int to 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]运算符表示在testArray ith元素上获取值,该元素是int 因此,您会遇到该错误。

为了澄清东西时, [...]在线路int testArray[arraySize]; [...]在行result = algorithmTest(testArray[arraySize], arraySize);[...]不同 :第一个用于指示数组的大小,第二个用于访问元素。

查看AlgorithmTest的定义。 您需要一个int [](也称为int *)作为第一个参数,但是当您调用它时,您会给它一个实际的int

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM