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: How to check whether malloc has allocated memory properly in case ...If the issue is that you are likely to call malloc() with an argument of ... 0500, Eric Sosman <esosman@ieee-dot-org.invalid> wrote: (in c.l.c, c.l.f added for check ... segmentation fault (SIGSEGV) - comp.lang.fortranBut is there any software or package to check this progress? My OS is openSUSE ... > > It makes some sense for BIND(C) VALUE arguments, as C would > do it with a prototype in ... How to separate command line args - comp.lang.rexx... if written in C or C++) specifically converts the individual C arguments above ... java.programmer... that expects a certain number of command line arguments.I need to check ... intent(out) for pointer dummy argument - comp.lang.fortran ...Fortran 2003 added the ability to specify intent for pointer dummy arguments. ... odds that your code would still be wrong - just in ways that are harder to check. Sockets in gfortran? - comp.lang.fortranThis argument is visible on the C side, but it is an integer _value_ ... gcc compilers, presumably the hidden arguments are at the end of the argument list - I must check .... Modifying MEX arguments in place? - comp.soft-sys.matlab ...Is there no way I could modify an argument in-place? ... following: format debug a = rand(3,3) b = a(:,1) c ... If I changed my example function to check the input is a ... argument of type "double" is incompatible with parameter of type ...Hi : I'm trying to call imread with mexCallMATLAB but I always get the same error that can be ? . error: argument of type "double" is incompatible w... How to use Inline assembly on C64x+ - comp.dspHi, i am trying to write inline assembly in my C ... Check the SPRU187.pdf file for more details. Cheers ... Usually, the inline assembler supports GNU style argument ... Passing Variables to standalone compiled matlab function - comp ...To debug problems with too many input arguments, temporarily change your expected input ... Passing va_list by reference to a function - comp.lang.c ... How to pass enum as ... problem with mixed c and fortran code - comp.lang.fortran ...... fcall_1=cout return end $ cat c.c ... it's not that big of a deal because nobody ever checks ... Subsequent arguments are then shifted one argument to the ... Command Line Arguments in C - Cprogramming.comLearn how to accept command line arguments in C using the argv ... checks to ensure the user added the second argument, theoretically a file name. The program then checks to ... Command-Line Arguments (C#) - Microsoft Corporation: Software ...The parameter of the Main method is a String array that represents the command-line arguments. Usually you check for the existence of the arguments by testing the Length ... 7/26/2012 4:32:43 PM
|