[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Problem passing parameter with int argc, char* argv[]

Posted on 2005-04-14
16
Medium Priority
?
798 Views
Last Modified: 2012-05-05
I am trying to pass parameter to a prgram this is being compiled with gcc in windows and should under dos...

I have writtten:

int main(int argc, char* argv[])


and then:


if (argc==1)
      {
      info(); // where info shows information related to the app
      }


   if (argc==2)
   {
         if (argv[1]=="continue")
         {
         printf("\ Option was continue ");
         }
         else if (argv[1]=="pause")
         {
         printf("\Option was paused");
         }  
      else
      {
       printf("  Please give an option pause or continue);
       printf("\n This is the value in argv[1]: %s",argv[1]);
      }
   }
     


I have tried it several times, but it alwasys goes straight to the else block, even insede I can display arg[1] as an expected option puase or continue,


Am I doing something wrong with the          if (argv[1]=="continue")
or is there some other problem??

0
Comment
Question by:RobertoDeAlba
  • 10
  • 4
  • 2
16 Comments
 
LVL 22

Expert Comment

by:grg99
ID: 13784226
you can't use == to compare strings in your basic C.   You can in C++, but that's only because some special functions are linked up to the == operator.

In C use strcmp(), see the man page.
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 13784245
Hi RobertoDeAlba,
    You cannot directly equate strings in C :(
    Use strcmp(argc[1],"contine")

Bye
---
Harish
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 13784251
grg99,
    You are very fast!
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 37

Expert Comment

by:Harisha M G
ID: 13784269
RobertoDeAlba,
    You should use strcmpi(argc[1],"continue");
    which ignores the case. Otherwise your program will not work if
    progname Contine
    is the command line :)
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 13784318
RobertoDeAlba,

Here is the program...

main(int argc, char **argv)
{
    if (argc==1)
    {
        info(); // where info shows information related to the app
    }


    if (argc==2)
    {
        if (strcmpi(argv[1],"continue"))
        {
            printf("\ Option was continue ");
        }
        else if (strcmpi(argv[1],"pause"))
        {
            printf("\Option was paused");
        }
        else
        {
            printf("  Please give an option pause or continue);
            printf("\n This is the value in argv[1]: %s",argv[1]);
        }
    }
}
0
 

Author Comment

by:RobertoDeAlba
ID: 13784344
     if (strcmp(argc[1],"continue")

return this when compiling


cvcmos.c:65: error: subscripted value is neither array nor pointer
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 13784369
argv .. not argc
0
 
LVL 22

Assisted Solution

by:grg99
grg99 earned 248 total points
ID: 13784470
and strcmp returns ZERO when the params are equal, so you need to add a == 0, which is a bit counterintuitive.

0
 
LVL 37

Accepted Solution

by:
Harisha M G earned 252 total points
ID: 13784513
Yeah.. good point :^)

main(int argc, char **argv)
{
    if (argc==1)
    {
        info(); // where info shows information related to the app
    }


    if (argc==2)
    {
        if (!strcmpi(argv[1],"continue"))
        {
            printf("\ Option was continue ");
        }
        else if (!strcmpi(argv[1],"pause"))
        {
            printf("\Option was paused");
        }
        else
        {
            printf("  Please give an option pause or continue);
            printf("\n This is the value in argv[1]: %s",argv[1]);
        }
    }
}

is suffecient :)
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 13784539
Now test by giving

cvcmos PAUSE
cvcmos Continue
cvcmos pause
cvcmos CONTINUE

!!
0
 

Author Comment

by:RobertoDeAlba
ID: 13785149
Yea it worked

But only with strcmp,   When strcmpi was used , this was returned from compiler:

c:/djgpp/tmp/ccGjP6iH.o(.text+0x1f7):cvcmos.c: undefined reference to `_strcmpi'


Any idea?
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 13785163
Which compiler are you using?
0
 

Author Comment

by:RobertoDeAlba
ID: 13785184
GCC, I not sure how to get the version

Under Windows, and output runs under DOS
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 13785204
I think that is available only in DOS.
Try using stricmp and strnicmp
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 13785218
Have you included String.H ?
#include <string.h>
0
 

Author Comment

by:RobertoDeAlba
ID: 13785266
Thanks!!!

mgh_mgharish   and grg99


0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question