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

Recommended Answers

All 2 Replies

Hello,
I have tested Your code(without the init_allocator(b,s) function).
And it worked out perfectly for me!
This is what I compiled, with gcc 4.5.3:

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
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 ();
        }
 }
 printf("%d, %d",b,s);
 }

Here is the result:
me@mycomp ~ $ ./a.out -b 4123 -s 1000
4123, 1000me@mycomp ~ $

Hello,
I have tested Your code(without the init_allocator(b,s) function).
And it worked out perfectly for me!
This is what I compiled, with gcc 4.5.3:

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
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 ();
        }
 }
 printf("%d, %d",b,s);
 }

Here is the result:
me@mycomp ~ $ ./a.out -b 4123 -s 1000
4123, 1000me@mycomp ~ $

Thanks, well sort of.. You were right that the code compiles, my issue was the
a.out -b 4123 -s 1000
I was doing:
a.out filename -b 4123 -s 1000
And for some reason it compiled, but obvisouly didn't work with the flags and numbers. I realized what you had said you ran and what I had said and fixed my issue.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.