Thread: Questions regarding char *argv[ ]...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116

    Questions regarding char *argv[ ]...

    I'm working on a program that accepts command line arguments and I was wondering if it would be OK to hard code the search pattern for argv[ ] because you already know what it's going to be looking for.
    ex:
    Code:
    int main (int argc, char *argv[ ])
    {
            int i;
    
            for(i = 0; *argv[1][i] != " "; i++)
            {
                   if(*argv[1][i] == "-")
                            i++;
                   if(*argv[1][i] == "r")
                           //more statements here etc etc
            }
    }
    Is that an acceptable way to search for command line options?
    Is there a better way that anyone has found?
    Thanks for any advise.
    Last edited by MikeyIckey; 02-10-2009 at 02:02 PM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are comparing strings with characters. argv[x][y] is a single character. "x" is a string consisting of the letter x and the terminating zero-character. You can not compare strings this way in C (nor do you need to in this case).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    what you're trying to do is probably something like:
    Code:
      if(!strncmp(argv[1],"-r",3)) { ... }
    Note that command line processing functions already exist (getopt, etc.).
    This is generally a bad practice to impose the CL argument order (from the user point of view) but if for some reason you think this is acceptable (for instance for an automatic call by a tool and not by a human), then why not.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    Quote Originally Posted by root4 View Post
    what you're trying to do is probably something like:
    Code:
      if(!strncmp(argv[1],"-r",3)) { ... }
    Note that command line processing functions already exist (getopt, etc.).
    This is generally a bad practice to impose the CL argument order (from the user point of view) but if for some reason you think this is acceptable (for instance for an automatic call by a tool and not by a human), then why not.
    No, I expect a human to use my code. Thanks for the advice, I'll read up on that and similar functions.
    Thank you.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Also note that since argv is char**, you cannot do *argv[n][n]. argv[n] gives a char*, and argv[n][n] gives a char.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    Quote Originally Posted by Elysia View Post
    Also note that since argv is char**, you cannot do *argv[n][n]. argv[n] gives a char*, and argv[n][n] gives a char.
    OK, so I was way off.
    I thought (wrongfully so, apparently) that
    Code:
    *++argv[i][j]
    would point to the the j-th character of the i-th character of argv.
    I presume I am incorrect in this assumption?
    Thanks again.
    Last edited by MikeyIckey; 02-11-2009 at 12:58 AM. Reason: ugly, short and stupid comment.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Point to?
    argv[i][j] is the j-th character of i-th string. Basically.
    Don't forget to check argc to see the number of arguments actually passed.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    Quote Originally Posted by Elysia View Post
    Point to?
    argv[i][j] is the j-th character of i-th string. Basically.
    Don't forget to check argc to see the number of arguments actually passed.
    noted. Thanks for everyone's help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM

Tags for this Thread