2017-10-11 77 views
0

배열에서 주어진 정수를 검색하기 위해 C++ 프로그램을 작성하고 있는데, 프로그램을 디버깅하려고 할 때 Visual Studio (vs 2015 pro 버전을 사용 중입니다)가 디버그 어설 션 오류에 대해 불평합니다. : enter image description here디버그 어설 션이 실패했습니다! Expression : 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); 

그것은 잘 작동합니다.