C、为什么会出现分段错误(核心转储)
#include <cs50.h>
#include <stdio.h>
#include<ctype.h>
int main(int argc, string argv[])
{
int KEY;
if(isdigit(argv[1]))
{
KEY = (int)argv[1];
}
else
{
printf("Usage: ./caesar key");
}
printf("\n");
printf("%i\n",argc);
}
时找不到问题
如何修复它?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是一种具有各种语法糖功能的高级语言。
重要提示:
const char *
到int
的转换不能仅通过类型转换来完成。isdigit()
仅适用于int
(单个字符),不适用于const char *
它会导致 未定义的行为。argc
是指针数组的长度argv
string
不是main()
函数参数的有效类型,它应该是int main(int argc, char const **argv) { }
argv[1]
是否存在stderr
return EXIT_SUCCESS;
,它是在头文件stdlib.h
中定义的,或者使用return EXIT_FAILURE;
如果出现问题。最终代码
This is not a high-level language with various syntax sugar features.
Important Notes:
const char *
toint
is not done by just type-casting it.isdigit()
works only forint
(single characters), notconst char *
it will cause Undefined Behavior.argc
is the length of pointer arrayargv
string
is not a valid type formain()
function arguments, it should beint main(int argc, char const **argv) { }
argv[1]
even exists or notstderr
return EXIT_SUCCESS;
, which is defined in the header filestdlib.h
, when exiting the application, or usereturn EXIT_FAILURE;
if something goes wrong.Final Code