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