argument check in C

  • Follow


I am writing C program in Linux. I compile it with gcc. I wish to
define the variable value of top_or_pack according to the second
argument. I have the following code:

printf("\nargv[2] = %s\t\ttop_or_pack = %d\n", argv[2], top_or_pack);
if(argv[2] == "-top"){
  top_or_pack=1;
}else{
   if(argv[2] == "-pack"){
    top_or_pack=2;
   }else{
    help_message();
    return 0;
  }
}

See how it runs here:


# my_program   file_a   -top   file_b

argv[2] = -top          top_or_pack = 0

    Display help message


# my_program   file_a   -pack   file_b

argv[2] = -pack         top_or_pack = 0

    Display help message


# my_program   file_a   a   file_b

argv[2] = a             top_or_pack = 0

    Display help message


#

The value of argv[2] is successfully got from command line. But the
flow control judgement always assumes argv[2] is neither "-top" nor "-
pack".

I tried adding & or *, which means changing  if(argv[2] ==  into  if
(&argv[2] ==  or  if(*argv[2] == . But the result is still like this.

What's the error in my code? How to fix it? Thanks.
0
Reply chen_zhitao (70) 6/11/2009 1:42:06 AM

On Jun 10, 6:42=A0pm, Kuhl <chen_zhi...@yahoo.com> wrote:

> if(argv[2] =3D=3D "-top"){

> =A0 =A0if(argv[2] =3D=3D "-pack"){

What type is 'argv[2]'? What type is "-top" or "-pack"?

Consider:
int j=3D3;
int i=3D3;
if(&j =3D=3D &i) ...

This will be false all the time, right?

Don't compare pointers with '=3D=3D'. Use the appropriate function to
compare their contents, which in this case is probably 'strcmp'.

DS
0
Reply David 6/11/2009 2:13:27 AM


Kuhl <chen_zhitao@yahoo.com> writes:
<snip>
>... I have the following code:
>
> printf("\nargv[2] = %s\t\ttop_or_pack = %d\n", argv[2], top_or_pack);
> if(argv[2] == "-top"){
>   top_or_pack=1;
> }else{
>    if(argv[2] == "-pack"){
>     top_or_pack=2;
>    }else{
>     help_message();
>     return 0;
>   }
> }
<snip>
> What's the error in my code? How to fix it? Thanks.

Use strcmp to compare strings.

The test argv[2] == "-top" tests two pointers.  The pointer in argv[2]
will never compare equal to the pointer that results from the string
literal "-top".

-- 
Ben.
0
Reply ben.usenet (6516) 6/11/2009 2:17:44 AM

Hi, strcmp works. But there's a further question. I did not include
any file in my code, however strcmp still works. There's no definition
of strcmp in my code. So is strcmp a key word that can be recognized
by gcc directly? Thanks.
0
Reply chen_zhitao (70) 6/11/2009 3:43:14 AM

Kuhl wrote:
> Hi, strcmp works. But there's a further question. I did not include
> any file in my code, however strcmp still works. There's no definition
> of strcmp in my code. So is strcmp a key word that can be recognized
> by gcc directly? Thanks.

No it is not, it is just implicitly declared (in your code) and subsequently 
found in a standard library.
Crank up the compiler's warning level...

Bye, Jojo 

0
Reply nospam.jojo (1344) 6/11/2009 6:26:23 AM

Kuhl  <chen_zhitao@yahoo.com> wrote:
>Hi, strcmp works. But there's a further question. I did not include
>any file in my code, however strcmp still works.

You should #include <string.h> to use strcmp() ("man strcmp" will tell
you which headers you need.) 

Plus this is a great example of why you should turn up your warning
level on your compiler:

$ gcc foo.c

vs:

$ gcc -Wall foo.c
foo.c: In function 'main':
foo.c:3: warning: implicit declaration of function 'strcmp'

-Beej

0
Reply beej (444) 6/11/2009 7:32:19 AM

On Jun 10, 8:43=A0pm, Kuhl <chen_zhi...@yahoo.com> wrote:

> Hi, strcmp works. But there's a further question. I did not include
> any file in my code, however strcmp still works. There's no definition
> of strcmp in my code. So is strcmp a key word that can be recognized
> by gcc directly? Thanks.

It doesn't work. It should generate an error, but it doesn't. So you
broke the rules, and things didn't work. No big surprise there.

DS
0
Reply David 6/11/2009 12:16:03 PM

On 11 June, 13:16, David Schwartz <dav...@webmaster.com> wrote:
> On Jun 10, 8:43 pm, Kuhl <chen_zhi...@yahoo.com> wrote:
>
> > Hi, strcmp works. But there's a further question. I did not include
> > any file in my code, however strcmp still works. There's no definition
> > of strcmp in my code. So is strcmp a key word that can be recognized
> > by gcc directly? Thanks.
>
> It doesn't work. It should generate an error, but it doesn't.

What do you mean should , like some standard says that it
should ?

> So you
> broke the rules, and things didn't work. No big surprise there.

But he said that things did work.

--
Who's your mama?
0
Reply spibou (1037) 6/12/2009 4:35:03 AM

7 Replies
40 Views

(page loaded in 0.09 seconds)

Similiar Articles:













7/26/2012 4:32:43 PM


Reply: