1
votes

I wanted to give several arguments in the command line, like:

./programName -a 103 -p argument1,argument2,argument3

Then, I wanted to create several variables with values of these arguments: one integer variable for the number just after the flag -a, one integer variable for the number of arguments just after the flag -p and one array variable with all these arguments.

It would be always the same arguments (so my code can not manage other forms of input): -a (integer) -p (a list of arguments separated with ,)

My problem is that there is a segmentation fault. After several attempts to see where it occurs (using several printf), there is a segmentation fault when the variable a = 3 (so at the flag -p). And the segmentation occurs at the line "case 'p'".

I don't understand why this code accepts the argument "-a" but not "-p". And I don't think there is an error because of my code in the "case 'p'" because the segmentation fault occurs after that (on the line "case 'p'").

I hope you'll understand my problem and thank you for your help :).

EDIT:

Thanks to Mark Wilkins and ooga for their answers. The segmentation fault was indeed due to the individual elements of the array which wasn't initialized. If some people don't know how to efficiently initialize an array of strings, check this other question, it's well explained ;) :

Initialize array of strings

3
Are you sure you ar using char* chrp = argv[p+1];? Because there is no p defined... - Roxxorfreak
When code does number_a = strtol(argv[a+1],NULL,10);, it does not know argv[a+1]; is not NULL. Maybe use for( ;a < (argc-1); ) - chux - Reinstate Monica
have not gone through full but your break should be out of curly braces ? - Nikole
As you acknowledge, there are a lot of problem that work for this exact input but break on other input. For example number_p should be reset to 0 in the case P. If you have any args other than -a and -p then your for loop never advances (infinite loop). If you have -a or -p with no arguments following, then you dereference a null pointer. Etc. - M.M

3 Answers

1
votes

One issue that would result in a segmentation fault is the use of arg_p. It is declared as an array of char*, but the individual elements of the array are never initialized. The subsequent use of it will result in accessing an uninitialized pointer:

*(arg_p[b]+c) = *(chrp+c);
1
votes

Your segfault occurs whilst assembling your argument array. Try this instead:

    arg_p[0] = chrp;
    chrp2 = strchr(argv[a+1], ',');
    int b = 1;
    while (chrp2 != NULL) {
        *chrp2 = '\0';
        arg_p[b++] = chrp2 + 1;
        chrp2 = strchr(chrp2 + 1, ',');
    }

Also note that your arg_p array is declared locally to that block and will cease to exist after that block. You may want to declare a char** arg_p pointer and malloc the space. Remember to free it at the end.

0
votes

When you increment 'a' by 2, you're going to go off the end of the argv array. You only want to increment by 1 because it's indexing the words in the command line (separated by spaces) not the characters.