0

If I declare int key before my if (argc != 2), I get a segmentation fault (core dumped) message when I run the program with only one command-line argument.

However if I declared int key after the if statement, the program runs and prints out my message (Please include integer input on command line).

Does anybody know why?

Also, not really sure why I should return 1 if an incorrect number of command-line arguments was passed to my program.

Here's my code

int main(int argc, string argv[])
{
    int key = atoi(argv[1])

    if (argc != 2)
    {    
        printf("Input one key of integer value\n");
        return 1;
    }      
}

Thanks!

  • It would be more helpful if you could post your code here. – userXktape Jul 15 '14 at 13:53
  • Here it is, thanks – Adam Wilson Jul 15 '14 at 14:42
4

You have popped with many questions at the same time. Lets start with the simplest one.

What's the relation between return value of a function and command line arguments?

You don't return 1 because you have 1 command line argument. The return value of any function(including main()) is almost independent of the arguments passed (unless you do something like this inside your code).

#include <iostream>
using namespace std;
int main(int argc, string argv[])
{
//do something here
return argc;
}

Although there's no reason why one shall do it. I gave it to just explain you when return values could depend upon number of arguments(argc here). In general, they don't depend, return anything you wish from a function(See below).

So should I return arbitrary values from main() ?

I take my words back now. No, you should not return any arbitrary value from main(). Read workmad3's answer. The return value tells the system how the program exited. In our case(pset2, caesar), we return the value 1 when we don't get enough/expected number of command line arguments because we can't process the data further(as it's insufficient). The cs50 graders are designed to detect the value 1 if and only if anything except 2 arguments were passed. Then the grader responds positively. The same case would have been with the value 2 or 3 if graders were designed so(and of course we were also told about that to return 2 or 3 or something else when certain conditions are not met).

What's the case with your code?

In your code, you write int key = atoi(argv[1]) before testing whether really there are 2 arguments passed to main(). If there were actually 2 or more arguments, then it was fine, but what when there was only 1 argument? In that case argv[1] is empty and you are trying to access its value. As a result it gives Segmentation Fault, which is generally encountered when you try to access a memory location with nothing in it(say NULL).

This is the answer to the question before it was edited.

| improve this answer | |
1

Mate, I understand at times silly things turns frustrating. Especially when working on big project. But you need to take care of small things. As I noticed in the following line

int key = atoi(argv[1])

you forgot semi-colon. Please correct the line as follows

int key = atoi(argv[1]);

Next, assume that user simply forgot about the command line arguments.

In that case.

argv[0] = /* ./programName */

argv[1] = ??? /* No one knows */

But when you try to access a memory which has nothing to do with your program like argv[1] (since nothing was stored in it) it gives and error of segmentation core dumped (which means the same).

So is the reason you need to check for proper command line arguments first and then store the key in some integer variable.

My answer to your second question is that in reality you can return any value, but that is going to be ethically incorrect for programmers. The value has nothing to do with number of command line arguments.

Returning a '0' means everything went well and the program is terminated successfully after achieving its goal. Anything other than 0, in most cases '1' means that something went wrong in your program and it is terminated without accomplishing its task.

If you are still confused with it, please be patient. This will come in use when you will learn debugging in up-comming weeks. That is when you will be able to check if the program terminated successfully or not.

| improve this answer | |
1

Does anybody know why?

Because you're trying to access a string that doesn't really exist in case it's not passed as a command-line argument to your program! So you should first check whether you get the correct number of command-line arguments passed to your program then start dealing with them.

Also, not really sure why I should return 1 if an incorrect number of command-line arguments was passed to my program.

main(), like any other function, has a return type. It returns an int. You might be asking yourself why we would ever need main() to return a value. Well, this value can be utilized after your program terminates.

By convention, main() should return 0 in case your program exited normally (with no errors). Since an int can take one of 2^32 - 1 values, we can assign vaguely all sorts of errors.

In this case, we're returning 1 in case the number of command-line arguments passed to our program doesn't match our needs and so on!

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .