Skip to contentSkip to conversation

processing arguments

DISCUSSION ameer armaly 4 participants 4 messages Apr 23, 2004 – Apr 24, 2004

lore
Share & tools
lore
#0 lore raw

Hi all.
Let's say that I have the following c++ code:
#include <iostream>
int main(int argc, char *argv[])
{
if(argv[1]=="hello")
cout <<"Hello World!";
return 0;
}

When I try that, and I type:
./a.out hello

It doesn't give me any output.
Can someone explain how to process args properly?
Thanks,

Ameer

#1 lore raw

Ameer,
Your if stmt:

    if(argv[1] == "hello")

is only comparing the pointer values in memory. You need to use strncmp(3) to do comparison on strings.

Chuck

On Fri, 2004-04-23 at 16:44, ameer armaly wrote:

     Hi all.
     Let's say that I have the following c++ code:
     #include <iostream>
     int main(int argc, char *argv[])
     {
     if(argv[1]=="hello")
     cout <<"Hello World!";
     return 0;
     }


     When I try that, and I type:
     ./a.out hello

     It doesn't give me any output.
     Can someone explain how to process args properly?
     Thanks,


     Ameer

     -
     To unsubscribe from this list: send the line "unsubscribe linux-c-
     programming" in
     the body of a message to [email protected]
     More majordomo info at  http://vger.kernel.org/majordomo-info.html
#2 lore raw
In reply to ameer armaly

Hi,

Quoted 1 line

if(argv[1]=="hello")

man strcmp

you can not compare strings like yours..

regards, murat.

Signature · 18 lines
- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
 A. Murat Eren
 [email protected], [email protected]
 http://zion.comu.edu.tr/~evreniz/
 0x88FD9FC7, 
 910A FCB3 2AAB 4CA5 E4D9 EFFA 6555 A33A 88FD 9FC7
- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -

--
 free software is a matter of liberty,
 not price. to understand the concept,
 you should think of "free speech",
 not "free beer".
-
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#3 lore raw
In reply to ameer armaly

Hi,

Quoted 6 lines

When I try that, and I type:
./a.out hello

It doesn't give me any output.
Can someone explain how to process args properly?
Thanks,

args are no special data type, they are just two dimenstional char array,
all the strings functions apply to them. Like the others mentioned string compare should be done either charecter by charecter or use the lib function on strings.

cheers,
Vadi.