这个程序总是崩溃而不是让我输入参数,为什么?
#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
关于c - 我不明白为什么这个程序总是崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19072557/