2017-10-11 73 views
0

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

这是我的代码,它的相当简单:

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; 
} 
+0

你解决了这个问题吗?如果您在代码行中的“searchKey”之前添加“&”,结果如何? –

回答

0

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

只需更改代码 “scanf_s(” %d “searchKey)” 到:

scanf_s("%d", &searchKey); 

这将很好地工作。