some more questions about argv

Discussion in 'C Programming' started by Momo, Apr 15, 2005.

  1. Momo

    Momo Guest

    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
     
    Momo, Apr 15, 2005
    #1
    1. Advertising

  2. Momo

    pete Guest

    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
     
    pete, Apr 15, 2005
    #2
    1. Advertising

  3. On 15 Apr 2005 06:19:48 -0700, in comp.lang.c ,
    (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 =----
     
    Mark McIntyre, Apr 15, 2005
    #3
  4. Mark McIntyre <> writes:
    > On 15 Apr 2005 06:19:48 -0700, in comp.lang.c ,
    > (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) <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.
     
    Keith Thompson, Apr 15, 2005
    #4
  5. On Fri, 15 Apr 2005 21:33:49 GMT, in comp.lang.c , Keith Thompson
    <> wrote:

    >Mark McIntyre <> writes:
    >> On 15 Apr 2005 06:19:48 -0700, in comp.lang.c ,
    >> (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 =----
     
    Mark McIntyre, Apr 16, 2005
    #5
  6. Mark McIntyre <> writes:
    > On Fri, 15 Apr 2005 21:33:49 GMT, in comp.lang.c , Keith Thompson
    > <> 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) <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.
     
    Keith Thompson, Apr 16, 2005
    #6
  7. Momo

    pete Guest

    Keith Thompson wrote:
    >
    > Mark McIntyre <> writes:
    > > On Fri, 15 Apr 2005 21:33:49 GMT, in comp.lang.c , Keith Thompson
    > > <> 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
     
    pete, Apr 16, 2005
    #7
  8. On Sat, 16 Apr 2005 11:12:35 GMT, in comp.lang.c , Keith Thompson
    <> wrote:

    >Mark McIntyre <> writes:
    >> On Fri, 15 Apr 2005 21:33:49 GMT, in comp.lang.c , Keith Thompson
    >> <> 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 =----
     
    Mark McIntyre, Apr 16, 2005
    #8
  9. Mark McIntyre <> writes:
    > On Sat, 16 Apr 2005 11:12:35 GMT, in comp.lang.c , Keith Thompson
    > <> 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) <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.
     
    Keith Thompson, Apr 17, 2005
    #9
  10. On Sat, 16 Apr 2005 23:26:20 GMT, in comp.lang.c , Keith Thompson
    <> wrote:

    >Mark McIntyre <> writes:
    >> On Sat, 16 Apr 2005 11:12:35 GMT, in comp.lang.c , Keith Thompson
    >> <> 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 =----
     
    Mark McIntyre, Apr 17, 2005
    #10
  11. Mark McIntyre <> writes:
    > On Sat, 16 Apr 2005 23:26:20 GMT, in comp.lang.c , Keith Thompson
    > <> wrote:

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

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

    [...]
    >>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'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.

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


    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.

    --
    Keith Thompson (The_Other_Keith) <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.
     
    Keith Thompson, Apr 17, 2005
    #11
  12. Mark McIntyre <> wrote:
    > On Fri, 15 Apr 2005 21:33:49 GMT, in comp.lang.c , Keith Thompson
    > <> wrote:


    > >Mark McIntyre <> writes:
    > >> On 15 Apr 2005 06:19:48 -0700, in comp.lang.c ,
    > >> (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)
     
    William Ahern, Apr 17, 2005
    #12
  13. William Ahern <william@wilbur.25thandClement.com> writes:
    > Mark McIntyre <> wrote:
    >> On Fri, 15 Apr 2005 21:33:49 GMT, in comp.lang.c , Keith Thompson
    >> <> wrote:
    >> >Mark McIntyre <> writes:
    >> >> On 15 Apr 2005 06:19:48 -0700, in comp.lang.c ,
    >> >> (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.

    --
    Keith Thompson (The_Other_Keith) <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.
     
    Keith Thompson, Apr 17, 2005
    #13
  14. On Sun, 17 Apr 2005 00:41:42 GMT, in comp.lang.c , Keith Thompson
    <> wrote:

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

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

    >>
    >> *shrug*

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


    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
     
    Mark McIntyre, Apr 17, 2005
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bret

    char **argv vs. char* argv[]

    Bret, Aug 31, 2003, in forum: C Programming
    Replies:
    21
    Views:
    4,602
    Richard Heathfield
    Sep 3, 2003
  2. David
    Replies:
    10
    Views:
    5,967
    Richard Heathfield
    Sep 15, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,650
    Old Wolf
    Jan 20, 2004
  4. =?ISO-8859-1?Q?Thomas_N=FCcker?=

    sys.argv[0] - 'module' object has no attribute 'argv'

    =?ISO-8859-1?Q?Thomas_N=FCcker?=, Jun 30, 2003, in forum: Python
    Replies:
    0
    Views:
    914
    =?ISO-8859-1?Q?Thomas_N=FCcker?=
    Jun 30, 2003
  5. Momo

    some more questions about argv

    Momo, Apr 16, 2005, in forum: C Programming
    Replies:
    2
    Views:
    329
    Al Bowers
    Apr 16, 2005
Loading...

Share This Page