Thread: argc/v question?

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    5

    argc/v question?

    here is my partial code:

    Code:
    int main(int argc, char *argv[])
    {
    
    	// make sure that there is at least 1 argument
    	if (argc < 2) {
    		usage();
    	}
    
    	switch (*argc[0]) 
    	{
    		case "-i":
    			myFunction(argc[0]);
    i get many errors. i am trying to input the arguments as strings, and use the arguments in myFunction that i am calling within the switch, any helpful hints?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    i get many errors
    Maybe you should tell us what they are.

    I can tell you that one error definitely comes from
    Code:
    case "-i":
    You can't use string literals in case statements. To compare strings, you have to use the strcmp() function.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Oh, and
    Code:
    switch (*argc[0])
    is very wrong. Look at what variable you are using here.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    5
    Code:
    int main(int argc, char *argv[])
    {
    	// make sure that there is at least 1 argument
    	if (argc < 2) {
    		usage();
    	}
    	if (*argv[0] == "-i") {
    		i();
    	}
    	else if (*argv[0] == "-h") {
    	        h(*argv[2]);
    	}
    	else if (*argv[0] == "-r") {
    		r();
    	}
    	else {
    		usage();
    	}
    
    	return 0;
    }
    here are my warnings (were errors, switched to if, versus switch:

    test.c(7) : warning C4047: '==' : 'int ' differs in levels of indirection from 'char [3]'
    test.c(10) : warning C4047: '==' : 'int ' differs in levels of indirection from 'char [3]'
    test.c(11) : warning C4047: 'function' : 'char *' differs in levels of indirection from 'char '
    test.c(11) : warning C4024: 'portScan' : different types for formal and actual parameter 1
    test.c(13) : warning C4047: '==' : 'int ' differs in levels of indirection from 'char [3]'

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I think this is what you are trying to do:
    Code:
    int main(int argc, char *argv[])
    {
    	// make sure that there is at least 1 argument
    	if (argc < 2) {
    		usage();
    		return 0;  // quit the program
    	}
    	if (!strcmp(argv[1],"-i")) { // Check if the first argument is equal to -i
    		i();
    	}
    	else if (!strcmp(argv[1],"-h")) {
    	        h(argv[2]);
    	}
    	else if (!strcmp(argv[1],"-r"))  {
    		r();
    	}
    	else {
    		usage();
    		return 0;
    	}
    
    	return 0;
    }
    Note that you are misunderstanding the arguments. Let's say your program was run like the fillowing:
    myprogram.exe -h hello

    Now here is what your arguments look like:
    argv[0] = "myprogram.exe"
    argv[1] = "-h"
    argv[2] = "hello"

    In order to see if a string is equal to something, you cannot use ==, you need to use strcmp() like in my example.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Location
    Stockholm, Sweden
    Posts
    64
    And what happens when I do this:
    myprogram.exe -i -h hello -r

    Doesn't windows have an equivalent of getoptlong?

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Doesn't windows have an equivalent of getoptlong?
    Nothing standard, but there are people out there that have programmed the equivalent. codeproject has a couple examples.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM