Checking param string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • david

    Checking param string

    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.
  • Malcolm McLean

    #2
    Re: Checking param string


    "david" <David.Abdurach manov@gmail.com wrote in message
    news:175897e6-9331-45c6-aa8e-d7874341d999@60 g2000hsy.google groups.com...
    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.


    Comment

    • david

      #3
      Re: Checking param string

      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.

      Comment

      • Ben Bacarisse

        #4
        Re: Checking param string

        david <David.Abdurach manov@gmail.com 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.

        Comment

        • Kenny McCormack

          #5
          Re: Checking param string

          In article <db023a6c-f9f4-49e4-a675-41718bdcff97@q7 0g2000hsb.googl egroups.com>,
          david <David.Abdurach manov@gmail.com 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)

          Comment

          • david

            #6
            Re: Checking param string

            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)

            Comment

            • david

              #7
              Re: Checking param string

              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?

              Comment

              • Flash Gordon

                #8
                Re: Checking param string

                Malcolm McLean wrote, On 20/02/08 18:00:
                >
                "david" <David.Abdurach manov@gmail.com wrote in message
                news:175897e6-9331-45c6-aa8e-d7874341d999@60 g2000hsy.google groups.com...
                >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.progr ammer, possibly one of the GNU groups
                for getopt_long.
                --
                Flash Gordon

                Comment

                • Malcolm McLean

                  #9
                  Re: Checking param string


                  "Flash Gordon" <spam@flash-gordon.me.ukwro te in message
                  news:pqau85xsk6 .ln2@news.flash-gordon.me.uk...
                  Malcolm McLean wrote, On 20/02/08 18:00:
                  >>
                  >"david" <David.Abdurach manov@gmail.com wrote in message
                  >news:175897e 6-9331-45c6-aa8e-d7874341d999@60 g2000hsy.google groups.com...
                  >>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.progr ammer, 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.



                  Comment

                  • Flash Gordon

                    #10
                    Re: Checking param string

                    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

                    Comment

                    • Flash Gordon

                      #11
                      Re: Checking param string

                      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

                      Comment

                      • Flash Gordon

                        #12
                        Re: Checking param string

                        Malcolm McLean wrote, On 20/02/08 20:46:
                        >
                        "Flash Gordon" <spam@flash-gordon.me.ukwro te in message
                        news:pqau85xsk6 .ln2@news.flash-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.progr ammer, 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

                        Comment

                        • Default User

                          #13
                          Re: Checking param string

                          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

                          Comment

                          • CBFalconer

                            #14
                            Re: Checking param string

                            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.tu wien.ac.at/anton/mail-news-errors.html>
                            <http://www.netmeister. org/news/learn2quote2.ht ml>
                            <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

                            Comment

                            • CBFalconer

                              #15
                              Re: Checking param string

                              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

                              Comment

                              Working...