delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/05/27/07:08:44

Date: Wed, 27 May 1998 13:06:28 +0200 (MET DST)
From: Olivier Perron <perron AT art DOT alcatel DOT fr>
To: Clint Allen <clint DOT allen AT mci2000 DOT com>
Cc: djgpp AT delorie DOT com
Subject: Re: C command line options
In-Reply-To: <CyQa1.113$mR4.91692@news.internetmci.com>
Message-Id: <Pine.GSO.3.96.980527130102.10084A-100000@rtbsci143s>
Mime-Version: 1.0

On Wed, 27 May 1998, Clint Allen wrote:

> Anyone who can help on this?
> I am trying to use command line options in my program.  Here's what I've got
> so far:
> 
> int main(int argc, char* argv[])
> {
> // Declare/init. variables
> int title_on;
> 
> // Command line option procedure
> if (argc == 1)
> {
>   title_on = 1;
> }
> else
> {
>   if (argv[1] == "/nts")
>    title_on = 0;
>   else if (argv[1] == "/?")
>   {
>    puts("Command line options");
>    puts("-----------------------");
>    puts("/?  Display this list");
>    puts("/nts No title screen");
>    return 0;
>   }
>   else
>   {
>    puts("Invalid command line option.  Use \"/?\" to see valid options.");
>    return 0;
>   }
> }
> 
> The problem is that whenever one of the valid options is entered, the
> comparison statements (such as--if (argv[1] == "/nts")) don't work.  In
> other words, even though argv[1] DOES equal "/nts", the program goes past
> the "if" statement as if it didn't.  Am I overlooking the obvious here, or
> is my compiler screwed?  BTW, I'm using DJGPP & RHIDE.  All help is greatly
> appreciated.
> __________________________________________________________________

If the first argument *IS* "/nts" then argv[1] points (yes, it is a
pointer !)  to the first character of the string "/nts".
This means that *argv[1] = '/'.
If you want to make a comparison between the string pointed to by argv[1]
and "/nts" you have to use a statement like :

   if(strcmp(argv[1], "/nts") == 0)


Olivier Perron.


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019