Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Checking param string

Reply
Thread Tools

Checking param string

 
 
david
Guest
Posts: n/a
 
      02-20-2008
Code:
#include <stdio.h>
#include <strings.h>
#include <stdlib.h>

int main (int argc, char const *argv[])
{
if (argc == 1)
printf("No parameters! Use --help to get more information.");

if (argc == 2 && argv[1] == "--help")
printf("Just test, you are free now.");

if (argv[1][0] == "-")
printf("This does not work?");

return 0;
}

Two questions:
1) How can I compare parameters to string, maybe I should use sprintf
and later try comparing? Or maybe there is direct way of doing it?
2) How can I get first char of the first parameter? (It's kinda hard
to understand how to write this)

Using gcc 4 version under Mac OS X.
 
Reply With Quote
 
 
 
 
Malcolm McLean
Guest
Posts: n/a
 
      02-20-2008

"david" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Code:
> #include <stdio.h>
> #include <strings.h>
> #include <stdlib.h>
>
> int main (int argc, char const *argv[])
> {
> if (argc == 1)
> printf("No parameters! Use --help to get more information.");
>
> if (argc == 2 && argv[1] == "--help")
> printf("Just test, you are free now.");
>
> if (argv[1][0] == "-")
> printf("This does not work?");
>
> return 0;
> }
>
> Two questions:
> 1) How can I compare parameters to string, maybe I should use sprintf
> and later try comparing? Or maybe there is direct way of doing it?
> 2) How can I get first char of the first parameter? (It's kinda hard
> to understand how to write this)
>

The compiler should give you a warning for the second and last. You are
comparing a character to a string. You need '-' to create a char constant,
strcmp() to compare
if( strcmp(argv[1], "-help") == 0)
{

}


When you've sorted this out you might like to see my options parser, on the
website.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

 
Reply With Quote
 
 
 
 
david
Guest
Posts: n/a
 
      02-20-2008
I understood the problem with string comparison, but how about the
last if, which makes gcc to give me warning: testas.c:13: warning:
comparison between pointer and integer

Can I access the certain char of that parameter or I should use some
strings commands to make this work?
argv[1] is a pointer to the char array in memory, *argv[1] should show
to the first char in that array, the same as *argv[1][0]. I am right
(It looks that I am not)

david

P.S. Thanks for the help.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-20-2008
david <(E-Mail Removed)> writes:

> I understood the problem with string comparison, but how about the
> last if, which makes gcc to give me warning: testas.c:13: warning:
> comparison between pointer and integer


The code is:

if (argv[1][0] == "-")
printf("This does not work?");

(you should quote enough to give context)

> Can I access the certain char of that parameter or I should use some
> strings commands to make this work?


You can get at the characters one by one if you like as arg[1][0] and
argv[1][1] etc. The error is with the "-" which is a string literal,
not a character. Use '-'.

> argv[1] is a pointer to the char array in memory, *argv[1] should show
> to the first char in that array, the same as *argv[1][0]. I am right
> (It looks that I am not)


The last one is not right but *argv[1] is OK. *argv[1][0] is trying
to apply * to character (namely argv[1][0]).

--
Ben.
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      02-20-2008
In article <(E-Mail Removed)>,
david <(E-Mail Removed)> wrote:
>I understood the problem with string comparison, but how about the
>last if, which makes gcc to give me warning: testas.c:13: warning:
>comparison between pointer and integer
>
>Can I access the certain char of that parameter or I should use some
>strings commands to make this work?
>argv[1] is a pointer to the char array in memory, *argv[1] should show
>to the first char in that array, the same as *argv[1][0]. I am right
>(It looks that I am not)
>
>david
>
>P.S. Thanks for the help.


I suggest you switch to a more friendly language, such as AWK.
(Where this sort of nonsense doesn't happen)

 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      02-20-2008
Malcolm McLean wrote, On 20/02/08 18:00:
>
> "david" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Code:
>> #include <stdio.h>
>> #include <strings.h>
>> #include <stdlib.h>
>>
>> int main (int argc, char const *argv[])
>> {
>> if (argc == 1)
>> printf("No parameters! Use --help to get more information.");


What if argc is 0? Yes, that *is* possible on any Unix like system,
which includes MacOS X

>> if (argc == 2 && argv[1] == "--help")
>> printf("Just test, you are free now.");
>>
>> if (argv[1][0] == "-")
>> printf("This does not work?");
>>
>> return 0;
>> }
>>
>> Two questions:
>> 1) How can I compare parameters to string, maybe I should use sprintf
>> and later try comparing? Or maybe there is direct way of doing it?
>> 2) How can I get first char of the first parameter? (It's kinda hard
>> to understand how to write this)
>>

> The compiler should give you a warning for the second and last. You are
> comparing a character to a string. You need '-' to create a char
> constant, strcmp() to compare
> if( strcmp(argv[1], "-help") == 0)
> {
>
> }
>
> When you've sorted this out you might like to see my options parser, on
> the website.


The OP is more likely to find the POSIX function getopt or the GNU
function getopt_long useful since he is almost certainly trying to
replicate their behaviour. For help with the POSIX getopt function the
OP should ask in comp.unix.programmer, possibly one of the GNU groups
for getopt_long.
--
Flash Gordon
 
Reply With Quote
 
david
Guest
Posts: n/a
 
      02-20-2008
I can't switch (studying Software Engineering). I just started to
learn C and C++ and difference between them.
C looks a bit familiar to ASM after some time, but I still don't know
this well enough for now, but that should change in month or two. But
I still think that ASM is better for now (for small code, like base64,
crc and etc programs/code fragments)
 
Reply With Quote
 
Amandil
Guest
Posts: n/a
 
      02-20-2008
On Feb 20, 12:50 pm, david <(E-Mail Removed)> wrote:
> Code:
> #include <stdio.h>
> #include <strings.h>
> #include <stdlib.h>
>
> int main (int argc, char const *argv[])
> {
> if (argc == 1)
> printf("No parameters! Use --help to get more information.");
>
> if (argc == 2 && argv[1] == "--help")


You mean:
if (argc == 2 && strcmp(argv[1], "--help") == 0)
You might also want an 'else if'.

> printf("Just test, you are free now.");
>
> if (argv[1][0] == "-")


Here you almost definitely want an else if:
else if (argv[1][0] == '-')
> printf("This does not work?");
>


Without the else, the user running the program with "--help" as the
argument will also cause the last if to be true, something you
probably don't want.

> return 0;
>
> }
>
> Two questions:
> 1) How can I compare parameters to string, maybe I should use sprintf
> and later try comparing? Or maybe there is direct way of doing it?


Use strcmp() and check the return value. strcmp() returns 0 when the 2
pointers passed contain the same string.

> 2) How can I get first char of the first parameter? (It's kinda hard
> to understand how to write this)
>
> Using gcc 4 version under Mac OS X.


As a general matter, there are some getopt libraries you can use. gcc
probably comes with one. If you can't use the library as is, you can
at least adapt some of the code to your own use. K&R2 also has (in the
chapter on pointers) a small example of reading command line options.

BTW, the reason you got a warning was trying to compare argv[1][0] (a
char, which is promoted to an int) and "-" (which yields the address
of the string - a pointer)

Happy Programming!

-- Marty (Hopefully giving some helpful advice)
 
Reply With Quote
 
david
Guest
Posts: n/a
 
      02-20-2008
I know about the else, I just wrote it for small example not thinking
much about if statements. Next time (if there will be one I will try
not to do this and write as much correct code as I can).

I am reading this group from Google, there could I find FAQ of this
group?
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      02-20-2008
david wrote, On 20/02/08 19:59:
> I know about the else, I just wrote it for small example not thinking
> much about if statements. Next time (if there will be one I will try
> not to do this and write as much correct code as I can).


The more effort you put in to writing your code the more effort people
are likely to put in to helping you.

> I am reading this group from Google, there could I find FAQ of this
> group?


When I search for comp.lang.c FAQ in Google (rather than Google Groups)
it is the first hit. http://c-faq.com/
--
Flash Gordon
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HTML::Template->param() : You gave me an odd number of parameters to param()! Dave Perl Misc 5 04-26-2011 02:44 AM
Problem passing string param to query with IN statement Bobby Edward ASP .Net 1 04-24-2009 02:42 PM
Overload by deriv class param; call w base class param ectoplasm C++ 12 07-28-2005 08:20 AM
Global xsl:param containing xpath expression string kurt hansen XML 1 09-07-2004 03:54 PM
XSLT: How to replace param name with this param's value ? Geathaa XML 2 07-30-2003 06:48 AM



Advertisments