Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > some more questions about argv

Reply
Thread Tools

some more questions about argv

 
 
Momo
Guest
Posts: n/a
 
      04-15-2005
i have some code like :
char depend[] = "-depend";
if (argv[1] != depend)
{
printf("Incorrect input for argument line");
}else{
/** continue the program **/
}
I m just trying to check that if argv[1] is -depend
but somehow the result shows that its they dont actually equals even
tho i have already put -depend on argv
please help
 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      04-15-2005
Momo wrote:
>
> i have some code like :
> char depend[] = "-depend";
> if (argv[1] != depend)
> {
> printf("Incorrect input for argument line");
> }else{
> /** continue the program **/
> }
> I m just trying to check that if argv[1] is -depend
> but somehow the result shows that its they dont actually equals even
> tho i have already put -depend on argv
> please help


#include <string.h>

if (argc > 1) {
if (strcmp(argv[1]), "-depend") != 0) {
puts("Incorrect input for argument line");
}
}

--
pete
 
Reply With Quote
 
 
 
 
Mark McIntyre
Guest
Posts: n/a
 
      04-15-2005
On 15 Apr 2005 06:19:48 -0700, in comp.lang.c , http://www.velocityreviews.com/forums/(E-Mail Removed)
(Momo) wrote:

>i have some code like :
>char depend[] = "-depend";
>if (argv[1] != depend)


You can 't compare strings like that in C. Remember they're pointers
to blocks of memory. All you did was compare the value of the
pointers.
You need strncmp.


--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-15-2005
Mark McIntyre <(E-Mail Removed)> writes:
> On 15 Apr 2005 06:19:48 -0700, in comp.lang.c , (E-Mail Removed)
> (Momo) wrote:
>
>>i have some code like :
>>char depend[] = "-depend";
>>if (argv[1] != depend)

>
> You can 't compare strings like that in C. Remember they're pointers
> to blocks of memory. All you did was compare the value of the
> pointers.
> You need strncmp.


Why strncmp rather than strcmp? strncmp makes sense if you want to
determine whether the string starts with "-depend", but strcmp is
better if you want to know whether it equals "-depend" (which matches
what the OP appears to have been trying to do).

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      04-16-2005
On Fri, 15 Apr 2005 21:33:49 GMT, in comp.lang.c , Keith Thompson
<(E-Mail Removed)> wrote:

>Mark McIntyre <(E-Mail Removed)> writes:
>> On 15 Apr 2005 06:19:48 -0700, in comp.lang.c , (E-Mail Removed)
>> (Momo) wrote:
>>
>>>i have some code like :
>>>char depend[] = "-depend";
>>>if (argv[1] != depend)

>>
>> You can 't compare strings like that in C. Remember they're pointers
>> to blocks of memory. All you did was compare the value of the
>> pointers.
>> You need strncmp.

>
>Why strncmp rather than strcmp?


I learned long ago to always use the 'n' variants of string handlers
for data input by users. Its become force of habit.

>strncmp makes sense if you want to
>determine whether the string starts with "-depend",


I'd probably do strncmp(foo, "-depend ", strlen("-depend "));


--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-16-2005
Mark McIntyre <(E-Mail Removed)> writes:
> On Fri, 15 Apr 2005 21:33:49 GMT, in comp.lang.c , Keith Thompson
> <(E-Mail Removed)> wrote:

[...]
>>Why strncmp rather than strcmp?

>
> I learned long ago to always use the 'n' variants of string handlers
> for data input by users. Its become force of habit.


I don't see the point in this case. If you want to compare the
strings for equality (for example, if you want to accept "-depend" and
reject "-depend42" and "-dependability"), you have to use strcmp.

>>strncmp makes sense if you want to
>>determine whether the string starts with "-depend",

>n
> I'd probably do strncmp(foo, "-depend ", strlen("-depend "));


Why? Now you're checking whether the argument starts with "-depend ",
including the space.

You can use any solution you like if you're free to change the problem
definition.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      04-16-2005
Keith Thompson wrote:
>
> Mark McIntyre <(E-Mail Removed)> writes:
> > On Fri, 15 Apr 2005 21:33:49 GMT, in comp.lang.c , Keith Thompson
> > <(E-Mail Removed)> wrote:

> [...]
> >>Why strncmp rather than strcmp?

> >
> > I learned long ago to always use the 'n' variants of string handlers
> > for data input by users. Its become force of habit.

>
> I don't see the point in this case. If you want to compare the
> strings for equality (for example, if you want to accept "-depend" and
> reject "-depend42" and "-dependability"), you have to use strcmp.
>
> >>strncmp makes sense if you want to
> >>determine whether the string starts with "-depend",

> >n
> > I'd probably do strncmp(foo, "-depend ", strlen("-depend "));

>
> Why? Now you're checking whether the argument starts with "-depend ",
> including the space.


strncmp(foo, "-depend ", sizeof "-depend ");

strcmp is really the right tool for this job.

--
pete
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      04-16-2005
On Sat, 16 Apr 2005 11:12:35 GMT, in comp.lang.c , Keith Thompson
<(E-Mail Removed)> wrote:

>Mark McIntyre <(E-Mail Removed)> writes:
>> On Fri, 15 Apr 2005 21:33:49 GMT, in comp.lang.c , Keith Thompson
>> <(E-Mail Removed)> wrote:

>[...]
>>>Why strncmp rather than strcmp?

>>
>> I learned long ago to always use the 'n' variants of string handlers
>> for data input by users. Its become force of habit.

>
>I don't see the point in this case.


I didn 't say there was a point in this case. I said I almost always
use them.

>If you want to compare the
>strings for equality (for example, if you want to accept "-depend" and
>reject "-depend42" and "-dependability"), you have to use strcmp.


Actually you don't, but I'm sure you can work out why.

>> I'd probably do strncmp(foo, "-depend ", strlen("-depend "));

>
>Why? Now you're checking whether the argument starts with "-depend ",
>including the space.


which is almost certainly what you want, when you think about it.

>You can use any solution you like if you're free to change the problem
>definition.


This doesn't change it.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-16-2005
Mark McIntyre <(E-Mail Removed)> writes:
> On Sat, 16 Apr 2005 11:12:35 GMT, in comp.lang.c , Keith Thompson
> <(E-Mail Removed)> wrote:

[...]
>>If you want to compare the
>>strings for equality (for example, if you want to accept "-depend" and
>>reject "-depend42" and "-dependability"), you have to use strcmp.

>
> Actually you don't, but I'm sure you can work out why.


I'm sure you could do something equivalent to strcmp(), but since
strcmp() does *exactly* what's needed in this case, why bother?

>>> I'd probably do strncmp(foo, "-depend ", strlen("-depend "));

>>
>>Why? Now you're checking whether the argument starts with "-depend ",
>>including the space.

>
> which is almost certainly what you want, when you think about it.


I've thought about it, and it's almost certainly not what's wanted.

The OP's (incorrect) code was:

char depend[] = "-depend";
if (argv[1] != depend)
[...]

He clearly wanted to check whether the first argument to the program
was exactly equal (in the string comparison sense) to "-depend".

Slightly OT:
On the systems I use, and on all Unix-like systems (and probably on
Windows systems as well), invoking the program as
progname -depend foobar
will cause argv[0] to point to some representation of the name of the
program, argv[1] to point to "-depend", and argv[2] to point to
"foobar" (with argc==3). There will be no spaces in any of the
arguments unless the user goes out of his way to put them there.

Are you expecting argv[1] to point to "-depend foobar"? It almost
certainly won't (and if it does it should probably be flagged as an
error).

Admittedly the standard says very little about how command line
information is tranformed to argc and argv, but regardless of any
system-specific considerations, the OP was clearly trying to check for
the exact string "-depend", no more, no less, no trailing spaces.

>>You can use any solution you like if you're free to change the problem
>>definition.

>
> This doesn't change it.


Checking for a space at the end of the string doesn't change it?
I honestly don't understand what you're trying to say. Am I
missing something here?

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      04-16-2005
On Sat, 16 Apr 2005 23:26:20 GMT, in comp.lang.c , Keith Thompson
<(E-Mail Removed)> wrote:

>Mark McIntyre <(E-Mail Removed)> writes:
>> On Sat, 16 Apr 2005 11:12:35 GMT, in comp.lang.c , Keith Thompson
>> <(E-Mail Removed)> wrote:

>[...]
>>>If you want to compare the
>>>strings for equality (for example, if you want to accept "-depend" and
>>>reject "-depend42" and "-dependability"), you have to use strcmp.

>>
>> Actually you don't, but I'm sure you can work out why.

>
>I'm sure you could do something equivalent to strcmp(), but since
>strcmp() does *exactly* what's needed in this case, why bother?


*shrug*

>>>> I'd probably do strncmp(foo, "-depend ", strlen("-depend "));
>>>
>>>Why? Now you're checking whether the argument starts with "-depend ",
>>>including the space.

>>
>> which is almost certainly what you want, when you think about it.

>
>I've thought about it, and it's almost certainly not what's wanted.


You may well be right. I'm not particularly bothered tho, as it seems
to me that this has descended into a yah-boo contest.

>I honestly don't understand what you're trying to say. Am I
>missing something here?


No idea. Look under the sofa I often find things under there I didn't
even realise were missing.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
some more questions about argv Momo C Programming 2 04-16-2005 03:27 AM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
difference(s) between char** argv and char* argv[] David C Programming 10 09-15-2003 06:58 AM
char **argv vs. char* argv[] Bret C Programming 21 09-03-2003 03:24 AM
sys.argv[0] - 'module' object has no attribute 'argv' =?ISO-8859-1?Q?Thomas_N=FCcker?= Python 0 06-30-2003 02:07 PM



Advertisments