簡體   English   中英

我不斷收到分段錯誤錯誤,我不確定問題出在哪里

[英]I keep getting segmentation fault error and I'm not sure where the issue is

上下文:我有這個大學作業我應該像高峰時間游戲..我有一個停車場,里面有車,有一輛特定的車(用*符號)應該離開停車場(出口在中間線最左欄). 我應該把車開走,把它弄出來。 例如,這是一個給定的批次:

|1||1|| || || |
| || ||2||2||2|
| || ||*||*||*|
|3||3|| || || |
|3||3|| || || |

首先,我需要讓用戶選擇要移動的汽車,檢查它是否存在於停車場中,如果不存在,則再次要求他選擇汽車。 這是SelectCar函數:

char SelectCar(char lot[MAX_LOT_LENGTH][MAX_LOT_LENGTH], int Length){
    char car;
    bool exists = false;
    printf("Enter the car you want to move:\n");
    scanf("%c",&car);
    for (int i = 0; i< Length; i++){
        for (int j = 0; j<Length; j++){
            if (lot[i][j] == car) {
                exists = true;
                
            }
        }
    }
    
    if(!exists) {
        printf("Invalid car id! enter again:\n");
        car = SelectCar(lot,Length);
    }
    printf("car selected\n");
    return car;
}

當我運行代碼時,它跳過了scanf函數(它甚至不要求輸入汽車),所以當它進入 for 循環時, "car"甚至不存在,所以它重新輸入SelectCar函數,當它到達scanf (第二次)時它崩潰並給我這個錯誤“: /home/p10303/.vpl_launcher.sh: line 12: 1904456 Segmentation fault (core dumped)./vpl_execution

我不知道該怎么做(我什至不明白問題是什么)

注意:批次和長度由用戶定義和接收。

我不知道該怎么辦

節省時間並啟用所有編譯器警告以快速查看char car; ... scanf("%d",car); char car; ... scanf("%d",car);

scanf()中的"%d"需要一個指向int的匹配指針。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM