some more questions about argv

M

Momo

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
 
P

pete

Momo said:
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");
}
}
 
M

Mark McIntyre

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.
 
K

Keith Thompson

Mark McIntyre said:
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).
 
M

Mark McIntyre

Mark McIntyre said:
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 "));
 
K

Keith Thompson

Mark McIntyre said:
On Fri, 15 Apr 2005 21:33:49 GMT, in comp.lang.c , Keith Thompson


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.
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.
 
P

pete

Keith said:
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.


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.
 
M

Mark McIntyre

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.
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.
 
K

Keith Thompson

Mark McIntyre said:
On Sat, 16 Apr 2005 11:12:35 GMT, in comp.lang.c , Keith Thompson


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?
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.
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?
 
M

Mark McIntyre

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'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.
 
K

Keith Thompson

Mark McIntyre said:
On Sat, 16 Apr 2005 23:26:20 GMT, in comp.lang.c , Keith Thompson


*shrug*

May I infer from that that there is no reason not to use strcmp() in
this case?

If you're in the habit of using strncmp() in cases where strcmp() is
simpler and more correct, I suggest that that's a bad habit.

[...]
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'm not sure what you mean by that. Until now, I thought it was a
technical discussion about the correct way to implement a particular
piece of functionality in C.
No idea. Look under the sofa I often find things under there I didn't
even realise were missing.

I meant to ask whether I'm missing something meaningful in what you've
been saying. It appears I haven't, though I'd be glad to be
enlightened.
 
W

William Ahern

Mark McIntyre said:
On Fri, 15 Apr 2005 21:33:49 GMT, in comp.lang.c , Keith Thompson
Mark McIntyre said:
On 15 Apr 2005 06:19:48 -0700, in comp.lang.c , (e-mail address 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.

What's needed is something like

strlcmp(const char *, size_t, const char *, size_t)
 
K

Keith Thompson

William Ahern said:
Mark McIntyre said:
On 15 Apr 2005 06:19:48 -0700, in comp.lang.c , (e-mail address 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.

What's needed is something like

strlcmp(const char *, size_t, const char *, size_t)

That might be useful, but does it solve some problem in this case that
strcmp() doesn't?

I'm assuming that the intent is to determine whether the string
pointed to by argv[1] is equal (in string comparison terms) to
"-depend". We can safely assume that both argv[1] and "-depend" are
properly nul-terminated strings.

strlcmp() might be useful if you want to match any of "-d", "-de",
"-dep", "-depe", "-depen", or "-depend", but I don't think that's what
the OP was looking for.

If you were making a general comment rather than trying to apply it to
this example, then never mind.
 
M

Mark McIntyre

May I infer from that that there is no reason not to use strcmp() in
this case?

you may infer what you like, though mostly that I'm bored with the
discussion
If you're in the habit of using strncmp() in cases where strcmp() is
simpler and more correct,

I disagree that this is what I do.
I suggest that that's a bad habit.

You're welcome to your opinion tho.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top