Thread: argv?

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    21

    argv?

    Assuming the program name is "test.exe", why the if statement is not true when I enter "test hello"?
    Code:
    include <stdio.h>
    int main(int argc, char *argv[])
    {
     if(argv[1] == "hello")
      printf("%s", argv[1]);
     return 0;
    }

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You're comparing pointers. You need to #include <string.h> and use the strcmp function.
    Code:
    #include <stdio.h>
    #include <string.h>
    int main(int argc, char *argv[])
    {
     if( strcmp( argv[1], "hello") == 0 )
      printf("%s", argv[1]);
     return 0;
    }
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You can't test strings using ==, use strcmp in <string.h> instead:
    Code:
    if (strcmp(argv[1], "hello") == 0)
      printf("%s\n", argv[1]);
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Code:
    include <stdio.h>
    #define C(a,b) !strcmp(a,b)
    
    int main(int argc, char *argv[])
    {
     if(C(argv[1],"hello"))
      printf("%s", argv[1]);
     return 0;
    }

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    ROFL!

    So many replies at the same time.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >#define C(a,b) !strcmp(a,b)
    That's truly fugly.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Originally posted by Prelude
    >#define C(a,b) !strcmp(a,b)
    That's truly fugly.
    stfu noob.
    ive been writing in c since 1953.

  8. #8
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    OK. Two (edit: three) things wrong with that flame.

    1) You're talking to Prelude. She is not a 'noob'. She is probably one of the best programmers on this board.
    2) C was created in the 70s.
    3) It truly is 'fugly'.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  9. #9
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Ive always found it makes more sense, especially for new programmers. strcmp(x,y) == 0 seems like it should mean the strings don't compare.

    And I wrote a book on C. My surname is Kerningham, and I rox.

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    1) Using terms like: stfu, noob, rox, etc is equal to saying, "Please ignore me for I am lower the pond scum."

    2) So you wrote a book, you want a cookie or something? You know how many badly written books are in the market?

    3) I agree with Prelude and XSquared, truely fugly.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >And I wrote a book on C. My surname is Kerningham, and I rox.

    No matches for "Kerningham". Below are matches for "kernighan".
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>And I wrote a book on C. My surname is Kerningham, and I rox.
    Then why is you're e-mail address briancollins@blueyonder.co.uk?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  13. #13
    Registered User
    Join Date
    Sep 2003
    Posts
    21
    Hi guys, thx for fast replies, very much appreciated!!!

    Give Brian a break, he's afterall have gd intention.

    I've been getting compilation error: "parse error before xxx". What does it mean?

    PS: BTW is argv a matrix array of pointers?
    Last edited by EeeK; 09-06-2003 at 08:30 PM.

  14. #14
    Registered User
    Join Date
    Sep 2003
    Posts
    21
    If I've a ADT, and I've created a function in the main method to call the ADT, is that wrong? Coz I got this undefined reference compilation errors.

    Code:
    include <stdio.h>
    include "adt.h"
    
    void adt(void);
    
    int main()
    {
     adt();
     return 0;
    }
    
    void adt()
    {
     adt x;
    
     create(x);
     destroy(x);
     ...
    }

  15. #15
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Hard to say without seeing more of the implementation.
    Post more code.

    But I *will* tell you that assuming that argv[1] is valid is not very wise. If you call the application with no command line, it will crash.
    There are two mechanisms we have to tell us how many args there are. The most obvious is argc, which will be greater than 1 if any args were passed. The second is the fact that argv is an array of pointers to strings, and the end of the array is marked with a NULL pointer. So the proper way to handle it is something like:

    Code:
    int main()
    {
      if(argc>1)
     {
         for(argc = 1; argv[argc] != NULL; ++argc)
       {
         printf("Arg #%d: %s \n", argc, argv[argc]);
         // process each arg
       }
     }
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help getting a grib on argc and argv
    By elwad in forum C Programming
    Replies: 10
    Last Post: 05-01-2009, 10:13 AM
  2. argv
    By taurus in forum C Programming
    Replies: 15
    Last Post: 10-14-2007, 08:57 AM
  3. Using argc and argv data
    By Rad_Turnip in forum C Programming
    Replies: 4
    Last Post: 03-31-2006, 06:09 AM
  4. how do i? re: argc - argv
    By luigi40 in forum C Programming
    Replies: 2
    Last Post: 06-11-2004, 10:17 AM
  5. more argv and argc
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 09-08-2001, 11:04 PM