英文:
The memory that i am trying to access and i do not have access to
问题
int main(int argc, char *argv[])
{
int i;
int sum = 0;
if (argc == 1) {
printf("0\n");
return (0);
}
else {
for (i = 1; i < argc; i++) {
if (isdigit(*argv[i]) == 0) {
printf("Error\n");
return (1);
}
sum += atoi(argv[i]);
}
}
printf("%d\n", sum);
return (0);
}
当我给它的可执行文件传递任何参数时,上面的代码总是输出“Segmentation fault (core dumped)”,这意味着我试图访问一个我没有访问权限的内存位置。
我认为加上条件(i > argc)应该意味着我确保argc的值允许我访问argv[argc - 1],但我不明白错误的来源,这就是我需要一些帮助来理解的地方。
我期望程序将数字参数相加并输出它们的总和,并且如果存在非数字参数,则打印“Error”一次。
英文:
int main(int argc, char *argv[])
{
int i;
int sum = 0;
if (argc == 1) {
printf("0\n");
return (0);
}
else {
for (i = 1; i < argc; i++) {
if (isdigit(argv[i]) == 0) {
printf("Error\n");
return (1);
}
sum += atoi(argv[i]);
}
}
printf("%d\n", sum);
return (0);
}
When I give its executable any argument/s, the above code always outputs "Segmentation fault (core dumped)" which means that I'm trying to access a memory location which I don have access to.
I think that putting the condition (i > argc) should mean that I do make sure that argc is of value allows me to access argv[argc - 1], yet I don't see where the error comes from and this is my where i need some help to understand.
I am expecting the program to add the digits arguments and outputs its sum and to print "Error" once if there a non digit argument
答案1
得分: 2
以下是您发布的代码的修改部分:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
int i;
int sum = 0;
if (argc == 1) {
printf("请键入要求和的数字。示例:myapp 2 4 6");
return 1;
}
else {
for (i = 1; i < argc; i++) {
if (!isdigit(argv[i][0])) {
printf("错误:%s 不是数字!\n", argv[i]);
}
else {
sum += atoi(argv[i]);
}
}
}
printf("%d\n", sum);
return (0);
}
以下是对代码的修改概要:
- 您应该为使用的所有函数包括头文件(例如,#include <stdio.h>)。
- isdigit() 接受一个字符而不是字符串。
- 如果 argc 不大于 1,则向用户提供有意义的消息,并返回一个非零结果给系统。
- 您不一定需要退出程序来处理不良输入:您可以选择以某种方式标记错误并继续执行。这取决于您。
- 但如果您选择退出,返回一个非零结果给系统。例如,这可以让 shell 脚本确定您的应用程序是否返回了有效的总和。
英文:
Here are a few modifications to the code you posted:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
int i;
int sum = 0;
if (argc == 1) {
printf("Please enter numbers to sum. EXAMPLE: myapp 2 4 6");
return 1;
}
else {
for (i = 1; i < argc; i++) {
if (!isdigit(argv[i][0])) {
printf("Error: %s isn't numeric!\n", argv[i]);
}
else {
sum += atoi(argv[i]);
}
}
}
printf("%d\n", sum);
return (0);
}
- You should include headers (e.g. #include <stdio.h>) for all the functions you use.
- isdigit() accepts a character, not string.
- If argc isn't > 1, provide a meaningful message to the user, and return a nonzero result to the system.
- You don't necessarily need to exit for bad input: you might wish to somehow flag the error, and continue. It's up to you.
- But if you do exit, return a nonzero result to the system. For example, that could allow a shell script to determine whether or not your app returned a valid sum.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论