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 ;) :