简体   繁体   English

我不明白为什么这个程序一直崩溃

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

this program keeps crashing instead of letting me input arguments, why? 该程序不断崩溃,而不是让我输入参数,为什么?

#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;
}

I am using codeblocks. 我正在使用代码块。 But I also tried to run it with Notepad++. 但是我也尝试使用Notepad ++来运行它。 After I compiled it and when I run it it simply crashes: Name.exe has stopped working. 在编译它并运行它后,它只是崩溃了:Name.exe已停止工作。 Shouldn't it ask me to input arguments on command line? 它不应该让我在命令行上输入参数吗?

A program can't possibly crash before you enter the arguments, because you need to enter the arguments before the program starts. 在输入参数之前 ,程序不可能崩溃,因为您需要在程序启动之前输入参数。

That is: you don't run your program like this: 也就是说:您不会像这样运行程序:

Program.exe
12
hello

you need to run it like this: 您需要像这样运行它:

Program.exe 12 hello

If you are using an IDE(you probably do), you need to configure your IDE to add the arguments. 如果使用的是IDE(可能会使用),则需要配置IDE以添加参数。 How to do that depends on which IDE you use. 如何执行取决于您使用的IDE。 I assume you use Visual Studio - here is how to do it in Visual Studio: https://stackoverflow.com/a/3697320/794380 我假设您使用的是Visual Studio-这是在Visual Studio中执行的操作: https : //stackoverflow.com/a/3697320/794380

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM