调试断言失败!表达:result_pointer = nullptr

时间:2017-10-11 17:15:17

标签: c++ debugging visual-studio-2015 windows-10 assertion

我正在编写一个C ++程序来搜索数组中的给定整数,但是,当我尝试调试程序时,visual studio(我正在使用vs 2015 pro版本)抱怨调试断言失败: enter image description here

这是我的代码,非常简单:

int main() {
int searchArray[10] = { 324,4567,6789,5421345,7,65,8965,12,342,485 };
//use searchKey for the number to be found
//use location for the array index of the found value
int searchKey, location;

//write code to determine if integers entered by 
//the user are in searchArray
//initiate searchKey and location
searchKey = 0;
location = 0;
int n = sizeof(searchArray) / sizeof(searchArray[0]);
//let user define the search key, give -1 to quit
while (true)
{
    std::cout << "Enter an integer ('-1') to quit: ";
    scanf_s("%d", searchKey);
    std::cout << searchKey << "\n";
    if (searchKey == -1)
    {
        break;
    }
    for (location; location < n; location++)
    {
        if (searchArray[location] == searchKey)
        {
            break;
        }
        location = -1;
    }
    if (location != -1)
    {
        std::cout << searchKey << " is at location " << location << " in the array.\n";
    }
    else
    {
        std::cout << searchKey << " is not in the array.\n";
    }
}
return 0;
}

1 个答案:

答案 0 :(得分:0)

每个参数必须是指向与格式中的类型说明符对应的类型变量的指针。

只需更改代码&#34; scanf_s(&#34;%d&#34;,searchKey)&#34;到:

scanf_s("%d", &searchKey);

它会很好用。