簡體   English   中英

我不明白為什么這個程序一直崩潰

[英]I can't understand why this program keeps crashing

該程序不斷崩潰,而不是讓我輸入參數,為什么?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, const char* argv[]) {
    int shift = atoi(argv[1]);
    char message[256];
    strcpy(message, argv[2]);
    int i;

    for (i = 0; i < strlen(message); i++) {
        printf("%c", message[i] + shift);
    }
    putchar('\n');

    return 0;
}

我正在使用代碼塊。 但是我也嘗試使用Notepad ++來運行它。 在編譯它並運行它后,它只是崩潰了:Name.exe已停止工作。 它不應該讓我在命令行上輸入參數嗎?

在輸入參數之前 ,程序不可能崩潰,因為您需要在程序啟動之前輸入參數。

也就是說:您不會像這樣運行程序:

Program.exe
12
hello

您需要像這樣運行它:

Program.exe 12 hello

如果使用的是IDE(可能會使用),則需要配置IDE以添加參數。 如何執行取決於您使用的IDE。 我假設您使用的是Visual Studio-這是在Visual Studio中執行的操作: https : //stackoverflow.com/a/3697320/794380

暫無
暫無

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

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