Checking param string

Discussion in 'C Programming' started by david, Feb 20, 2008.

  1. david

    david Guest

    Code:
    #include <stdio.h>
    #include <strings.h>
    #include <stdlib.h>

    int main (int argc, char const *argv[])
    {
    if (argc == 1)
    printf("No parameters! Use --help to get more information.");

    if (argc == 2 && argv[1] == "--help")
    printf("Just test, you are free now.");

    if (argv[1][0] == "-")
    printf("This does not work?");

    return 0;
    }

    Two questions:
    1) How can I compare parameters to string, maybe I should use sprintf
    and later try comparing? Or maybe there is direct way of doing it?
    2) How can I get first char of the first parameter? (It's kinda hard
    to understand how to write this)

    Using gcc 4 version under Mac OS X.
     
    david, Feb 20, 2008
    #1
    1. Advertising

  2. "david" <> wrote in message
    news:...
    > Code:
    > #include <stdio.h>
    > #include <strings.h>
    > #include <stdlib.h>
    >
    > int main (int argc, char const *argv[])
    > {
    > if (argc == 1)
    > printf("No parameters! Use --help to get more information.");
    >
    > if (argc == 2 && argv[1] == "--help")
    > printf("Just test, you are free now.");
    >
    > if (argv[1][0] == "-")
    > printf("This does not work?");
    >
    > return 0;
    > }
    >
    > Two questions:
    > 1) How can I compare parameters to string, maybe I should use sprintf
    > and later try comparing? Or maybe there is direct way of doing it?
    > 2) How can I get first char of the first parameter? (It's kinda hard
    > to understand how to write this)
    >

    The compiler should give you a warning for the second and last. You are
    comparing a character to a string. You need '-' to create a char constant,
    strcmp() to compare
    if( strcmp(argv[1], "-help") == 0)
    {

    }


    When you've sorted this out you might like to see my options parser, on the
    website.
    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
     
    Malcolm McLean, Feb 20, 2008
    #2
    1. Advertising

  3. david

    david Guest

    I understood the problem with string comparison, but how about the
    last if, which makes gcc to give me warning: testas.c:13: warning:
    comparison between pointer and integer

    Can I access the certain char of that parameter or I should use some
    strings commands to make this work?
    argv[1] is a pointer to the char array in memory, *argv[1] should show
    to the first char in that array, the same as *argv[1][0]. I am right
    (It looks that I am not)

    david

    P.S. Thanks for the help.
     
    david, Feb 20, 2008
    #3
  4. david <> writes:

    > I understood the problem with string comparison, but how about the
    > last if, which makes gcc to give me warning: testas.c:13: warning:
    > comparison between pointer and integer


    The code is:

    if (argv[1][0] == "-")
    printf("This does not work?");

    (you should quote enough to give context)

    > Can I access the certain char of that parameter or I should use some
    > strings commands to make this work?


    You can get at the characters one by one if you like as arg[1][0] and
    argv[1][1] etc. The error is with the "-" which is a string literal,
    not a character. Use '-'.

    > argv[1] is a pointer to the char array in memory, *argv[1] should show
    > to the first char in that array, the same as *argv[1][0]. I am right
    > (It looks that I am not)


    The last one is not right but *argv[1] is OK. *argv[1][0] is trying
    to apply * to character (namely argv[1][0]).

    --
    Ben.
     
    Ben Bacarisse, Feb 20, 2008
    #4
  5. In article <>,
    david <> wrote:
    >I understood the problem with string comparison, but how about the
    >last if, which makes gcc to give me warning: testas.c:13: warning:
    >comparison between pointer and integer
    >
    >Can I access the certain char of that parameter or I should use some
    >strings commands to make this work?
    >argv[1] is a pointer to the char array in memory, *argv[1] should show
    >to the first char in that array, the same as *argv[1][0]. I am right
    >(It looks that I am not)
    >
    >david
    >
    >P.S. Thanks for the help.


    I suggest you switch to a more friendly language, such as AWK.
    (Where this sort of nonsense doesn't happen)
     
    Kenny McCormack, Feb 20, 2008
    #5
  6. david

    Flash Gordon Guest

    Malcolm McLean wrote, On 20/02/08 18:00:
    >
    > "david" <> wrote in message
    > news:...
    >> Code:
    >> #include <stdio.h>
    >> #include <strings.h>
    >> #include <stdlib.h>
    >>
    >> int main (int argc, char const *argv[])
    >> {
    >> if (argc == 1)
    >> printf("No parameters! Use --help to get more information.");


    What if argc is 0? Yes, that *is* possible on any Unix like system,
    which includes MacOS X

    >> if (argc == 2 && argv[1] == "--help")
    >> printf("Just test, you are free now.");
    >>
    >> if (argv[1][0] == "-")
    >> printf("This does not work?");
    >>
    >> return 0;
    >> }
    >>
    >> Two questions:
    >> 1) How can I compare parameters to string, maybe I should use sprintf
    >> and later try comparing? Or maybe there is direct way of doing it?
    >> 2) How can I get first char of the first parameter? (It's kinda hard
    >> to understand how to write this)
    >>

    > The compiler should give you a warning for the second and last. You are
    > comparing a character to a string. You need '-' to create a char
    > constant, strcmp() to compare
    > if( strcmp(argv[1], "-help") == 0)
    > {
    >
    > }
    >
    > When you've sorted this out you might like to see my options parser, on
    > the website.


    The OP is more likely to find the POSIX function getopt or the GNU
    function getopt_long useful since he is almost certainly trying to
    replicate their behaviour. For help with the POSIX getopt function the
    OP should ask in comp.unix.programmer, possibly one of the GNU groups
    for getopt_long.
    --
    Flash Gordon
     
    Flash Gordon, Feb 20, 2008
    #6
  7. david

    david Guest

    I can't switch (studying Software Engineering). I just started to
    learn C and C++ and difference between them.
    C looks a bit familiar to ASM after some time, but I still don't know
    this well enough for now, but that should change in month or two. But
    I still think that ASM is better for now (for small code, like base64,
    crc and etc programs/code fragments)
     
    david, Feb 20, 2008
    #7
  8. david

    Amandil Guest

    On Feb 20, 12:50 pm, david <> wrote:
    > Code:
    > #include <stdio.h>
    > #include <strings.h>
    > #include <stdlib.h>
    >
    > int main (int argc, char const *argv[])
    > {
    > if (argc == 1)
    > printf("No parameters! Use --help to get more information.");
    >
    > if (argc == 2 && argv[1] == "--help")


    You mean:
    if (argc == 2 && strcmp(argv[1], "--help") == 0)
    You might also want an 'else if'.

    > printf("Just test, you are free now.");
    >
    > if (argv[1][0] == "-")


    Here you almost definitely want an else if:
    else if (argv[1][0] == '-')
    > printf("This does not work?");
    >


    Without the else, the user running the program with "--help" as the
    argument will also cause the last if to be true, something you
    probably don't want.

    > return 0;
    >
    > }
    >
    > Two questions:
    > 1) How can I compare parameters to string, maybe I should use sprintf
    > and later try comparing? Or maybe there is direct way of doing it?


    Use strcmp() and check the return value. strcmp() returns 0 when the 2
    pointers passed contain the same string.

    > 2) How can I get first char of the first parameter? (It's kinda hard
    > to understand how to write this)
    >
    > Using gcc 4 version under Mac OS X.


    As a general matter, there are some getopt libraries you can use. gcc
    probably comes with one. If you can't use the library as is, you can
    at least adapt some of the code to your own use. K&R2 also has (in the
    chapter on pointers) a small example of reading command line options.

    BTW, the reason you got a warning was trying to compare argv[1][0] (a
    char, which is promoted to an int) and "-" (which yields the address
    of the string - a pointer)

    Happy Programming!

    -- Marty (Hopefully giving some helpful advice)
     
    Amandil, Feb 20, 2008
    #8
  9. david

    david Guest

    I know about the else, I just wrote it for small example not thinking
    much about if statements. Next time (if there will be one I will try
    not to do this and write as much correct code as I can).

    I am reading this group from Google, there could I find FAQ of this
    group?
     
    david, Feb 20, 2008
    #9
  10. david

    Flash Gordon Guest

    david wrote, On 20/02/08 19:59:
    > I know about the else, I just wrote it for small example not thinking
    > much about if statements. Next time (if there will be one I will try
    > not to do this and write as much correct code as I can).


    The more effort you put in to writing your code the more effort people
    are likely to put in to helping you.

    > I am reading this group from Google, there could I find FAQ of this
    > group?


    When I search for comp.lang.c FAQ in Google (rather than Google Groups)
    it is the first hit. http://c-faq.com/
    --
    Flash Gordon
     
    Flash Gordon, Feb 20, 2008
    #10
  11. david

    Flash Gordon Guest

    david wrote, On 20/02/08 19:17:
    > I can't switch (studying Software Engineering). I just started to
    > learn C and C++ and difference between them.


    It is generally best to think of them as different languages so you
    don't get caught out by the differences. They just happen to share a lot
    of the same syntax, a syntax also used by Java and other languages.

    > C looks a bit familiar to ASM after some time, but I still don't know
    > this well enough for now, but that should change in month or two. But
    > I still think that ASM is better for now (for small code, like base64,
    > crc and etc programs/code fragments)


    Now try porting your base64 implementation to a different processor.
    Mine is currently running on x86 and PowerPC hardware without change
    where as yours will have to be re-written.
    --
    Flash Gordon
     
    Flash Gordon, Feb 20, 2008
    #11
  12. "Flash Gordon" <> wrote in message
    news:p-gordon.me.uk...
    > Malcolm McLean wrote, On 20/02/08 18:00:
    >>
    >> "david" <> wrote in message
    >> news:...
    >>> Code:
    >>> #include <stdio.h>
    >>> #include <strings.h>
    >>> #include <stdlib.h>
    >>>
    >>> int main (int argc, char const *argv[])
    >>> {
    >>> if (argc == 1)
    >>> printf("No parameters! Use --help to get more information.");

    >
    > What if argc is 0? Yes, that *is* possible on any Unix like system, which
    > includes MacOS X
    >
    >>> if (argc == 2 && argv[1] == "--help")
    >>> printf("Just test, you are free now.");
    >>>
    >>> if (argv[1][0] == "-")
    >>> printf("This does not work?");
    >>>
    >>> return 0;
    >>> }
    >>>
    >>> Two questions:
    >>> 1) How can I compare parameters to string, maybe I should use sprintf
    >>> and later try comparing? Or maybe there is direct way of doing it?
    >>> 2) How can I get first char of the first parameter? (It's kinda hard
    >>> to understand how to write this)
    >>>

    >> The compiler should give you a warning for the second and last. You are
    >> comparing a character to a string. You need '-' to create a char
    >> constant, strcmp() to compare
    >> if( strcmp(argv[1], "-help") == 0)
    >> {
    >>
    >> }
    >>
    >> When you've sorted this out you might like to see my options parser, on
    >> the website.

    >
    > The OP is more likely to find the POSIX function getopt or the GNU
    > function getopt_long useful since he is almost certainly trying to
    > replicate their behaviour. For help with the POSIX getopt function the OP
    > should ask in comp.unix.programmer, possibly one of the GNU groups for
    > getopt_long.
    >

    My options parser depends only on the standard library. The alternatives you
    suggest may not be available on the OP's platform.

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
     
    Malcolm McLean, Feb 20, 2008
    #12
  13. david

    Flash Gordon Guest

    Malcolm McLean wrote, On 20/02/08 20:46:
    >
    > "Flash Gordon" <> wrote in message
    > news:p-gordon.me.uk...
    >> Malcolm McLean wrote, On 20/02/08 18:00:


    <snip>

    >>> When you've sorted this out you might like to see my options parser,
    >>> on the website.

    >>
    >> The OP is more likely to find the POSIX function getopt or the GNU
    >> function getopt_long useful since he is almost certainly trying to
    >> replicate their behaviour. For help with the POSIX getopt function the
    >> OP should ask in comp.unix.programmer, possibly one of the GNU groups
    >> for getopt_long.
    >>

    > My options parser depends only on the standard library. The alternatives
    > you suggest may not be available on the OP's platform.


    The OP stated he is using MacOS X and that is a Unix variant and
    therefore definitely has getopt as I stated. I'm not certain about
    getopt_long (hence the reference to GNU groups) but it is highly likely
    and if not the sources *are* easily available in standard C in a number
    of places.

    Far better to use a function that will give the same behaviour as most
    the other applications on the target box and is used so heavily that it
    is extensively tested than to use a lightly used package that is less
    likely to produce identical behaviour.
    --
    Flash Gordon
     
    Flash Gordon, Feb 20, 2008
    #13
  14. david

    Default User Guest

    david wrote:

    > I know about the else, I just wrote it for small example not thinking
    > much about if statements. Next time (if there will be one I will try
    > not to do this and write as much correct code as I can).
    >
    > I am reading this group from Google, there could I find FAQ of this
    > group?


    Did you trying searching for: comp.lang.c faq ?




    Brian
     
    Default User, Feb 20, 2008
    #14
  15. david

    CBFalconer Guest

    david wrote:
    >
    > Code:
    > #include <stdio.h>
    > #include <strings.h>
    > #include <stdlib.h>
    >
    > int main (int argc, char const *argv[]) {
    > if (argc == 1)


    Use "<=" here. Who says argc can't be zero?

    > printf("No parameters! Use --help to get more information.");
    >
    > if (argc == 2 && argv[1] == "--help")
    > printf("Just test, you are free now.");
    >
    > if (argv[1][0] == "-")
    > printf("This does not work?");
    >
    > return 0;
    > }
    >
    > Two questions:
    > 1) How can I compare parameters to string, maybe I should use
    > sprintf and later try comparing? Or maybe there is direct way
    > of doing it?


    Look up the strcmp function.

    > 2) How can I get first char of the first parameter? (It's kinda hard
    > to understand how to write this)


    if (argv[1][0] == '-') ...

    Note use of ' for chars, not " for strings.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Feb 21, 2008
    #15
  16. david

    CBFalconer Guest

    david wrote:
    >
    > I can't switch (studying Software Engineering). I just started to
    > learn C and C++ and difference between them. C looks a bit
    > familiar to ASM after some time, but I still don't know this well
    > enough for now, but that should change in month or two. But I
    > still think that ASM is better for now (for small code, like
    > base64, crc and etc programs/code fragments)


    Some useful links on quoting:
    <http://www.xs4all.nl/%7ewijnands/nnq/nquote.html>
    <http://www.complang.tuwien.ac.at/anton/mail-news-errors.html>
    <http://www.netmeister.org/news/learn2quote2.html>
    <http://www.star-one.org.uk/computer/format.htm>

    Usenet is a 'best efforts' protocol. There is no guarantee that
    your reader can ever read any other message in a thread. Thus you
    should always quote enough for your message to stand entirely by
    itself.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Feb 21, 2008
    #16
  17. david

    CBFalconer Guest

    Flash Gordon wrote:
    > Malcolm McLean wrote, On 20/02/08 18:00:
    >> "david" <> wrote in message
    >>

    .... snip ...
    >>>
    >>> Two questions:
    >>> 1) How can I compare parameters to string, maybe I should use
    >>> sprintf and later try comparing? Or maybe there is direct
    >>> way of doing it?
    >>> 2) How can I get first char of the first parameter? (It's
    >>> kinda hard to understand how to write this)

    >>

    .... snip ...
    >>
    >> When you've sorted this out you might like to see my options
    >> parser, on the website.

    >
    > The OP is more likely to find the POSIX function getopt or the
    > GNU function getopt_long useful since he is almost certainly
    > trying to replicate their behaviour. For help with the POSIX
    > getopt function the OP should ask in comp.unix.programmer,
    > possibly one of the GNU groups for getopt_long.


    No he shouldn't. He should learn to do these basic operations long
    before using those.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Feb 21, 2008
    #17
  18. david

    CBFalconer Guest

    Default User wrote:
    > david wrote:
    >
    >> I know about the else, I just wrote it for small example not
    >> thinking much about if statements. Next time (if there will be
    >> one I will try not to do this and write as much correct code as
    >> I can). I am reading this group from Google, there could I
    >> find FAQ of this group?

    >
    > Did you trying searching for: comp.lang.c faq ?


    Some useful references about C:
    <http://www.ungerhu.com/jxh/clc.welcome.txt>
    <http://c-faq.com/> (C-faq)
    <http://benpfaff.org/writings/clc/off-topic.html>
    <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf> (C99)
    <http://cbfalconer.home.att.net/download/n869_txt.bz2> (C99, txt)
    <http://www.dinkumware.com/refxc.html> (C-library}
    <http://gcc.gnu.org/onlinedocs/> (GNU docs)

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Feb 21, 2008
    #18
  19. david

    Guest

    On Feb 21, 3:40 am, CBFalconer <> wrote:
    > Default User wrote:
    > > david wrote:

    >
    > >> I know about the else, I just wrote it for small example not
    > >> thinking much about if statements. Next time (if there will be
    > >> one I will try not to do this and write as much correct code as
    > >> I can). I am reading this group from Google, there could I
    > >> find FAQ of this group?

    >
    > > Did you trying searching for: comp.lang.c faq ?

    >
    > Some useful references about C:
    > <http://www.ungerhu.com/jxh/clc.welcome.txt>
    > <http://c-faq.com/> (C-faq)
    > <http://benpfaff.org/writings/clc/off-topic.html>
    > <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf> (C99)
    > <http://cbfalconer.home.att.net/download/n869_txt.bz2> (C99, txt)
    > <http://www.dinkumware.com/refxc.html> (C-library}

    Update your links, this one does not work.
    "The system cannot find the file specified."
    > <http://gcc.gnu.org/onlinedocs/> (GNU docs)
     
    , Feb 22, 2008
    #19
  20. david

    CBFalconer Guest

    wrote:
    > CBFalconer <> wrote:
    >> Default User wrote:
    >>> david wrote:

    >>
    >>>> I know about the else, I just wrote it for small example not
    >>>> thinking much about if statements. Next time (if there will be
    >>>> one I will try not to do this and write as much correct code as
    >>>> I can). I am reading this group from Google, there could I
    >>>> find FAQ of this group?

    >>
    >>> Did you trying searching for: comp.lang.c faq ?

    >>
    >> Some useful references about C:
    >> <http://www.ungerhu.com/jxh/clc.welcome.txt>
    >> <http://c-faq.com/> (C-faq)
    >> <http://benpfaff.org/writings/clc/off-topic.html>
    >> <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf> (C99)
    >> <http://cbfalconer.home.att.net/download/n869_txt.bz2> (C99, txt)
    >> <http://www.dinkumware.com/refxc.html> (C-library}

    >
    > Update your links, this one does not work.
    > "The system cannot find the file specified."
    >
    >> <http://gcc.gnu.org/onlinedocs/> (GNU docs)


    Changed to "www.dinkumware.com\c99.aspx". Thanks.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Feb 22, 2008
    #20
    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. Geathaa
    Replies:
    2
    Views:
    714
    Geathaa
    Jul 30, 2003
  2. kurt hansen
    Replies:
    1
    Views:
    1,048
    David Carlisle
    Sep 7, 2004
  3. ectoplasm
    Replies:
    12
    Views:
    655
    Zorro
    Jul 28, 2005
  4. Bobby Edward
    Replies:
    1
    Views:
    379
    imransyed63
    Apr 24, 2009
  5. Dave
    Replies:
    5
    Views:
    697
    John Bokma
    Apr 26, 2011
Loading...

Share This Page