I am trying to read in a command-line argument like:
>a.out memtest -b 4 -s 1000
And the intended result is to store "4" in the variable represented by -b and store "1000" in the variable represented by -s. I have the following code:
int main (int argc, char **argv){
int index, b, s;
int c;
while ((c = getopt (argc, argv, "b:s:")) > 0)
{
switch (c)
{
case 'b':
b = atoi(optarg);
break;
case 's':
s = atoi(optarg);
break;
case '?':
if (optopt == 'c')
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr,"Unknown option character `\\x%x'.\n",optopt);
return 1;
default:
abort ();
}
}
init_allocator(b,s);
printf("%d, %d",b,s);
ackerman_main();
testCode();
}
Basically I successfully store "4" in the int variable b but get a garbage value for s which should be 1000. The functions are exactly the same and nothing changes from b to s but it still gives me this garbage value. I assume it has to do with parsing the command-line correctly and once its completed what its doing on b it breaks for any vars afterwards. Can you spot any errors with my code?
Thanks