Checking param string

D

david

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.
 
M

Malcolm McLean

david said:
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.
 
D

david

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.
 
B

Ben Bacarisse

david said:
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]).
 
K

Kenny McCormack

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)
 
F

Flash Gordon

Malcolm McLean wrote, On 20/02/08 18:00:
david said:
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.
 
D

david

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)
 
A

Amandil

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)
 
D

david

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?
 
F

Flash Gordon

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/
 
F

Flash Gordon

david wrote, On 20/02/08 19:17:
I can't switch (studying Software Engineering). I just started to
learn C and C++ and difference between them.

It is generally best to think of them as different languages so you
don't get caught out by the differences. They just happen to share a lot
of the same syntax, a syntax also used by Java and other languages.
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)

Now try porting your base64 implementation to a different processor.
Mine is currently running on x86 and PowerPC hardware without change
where as yours will have to be re-written.
 
M

Malcolm McLean

Flash Gordon said:
Malcolm McLean wrote, On 20/02/08 18:00:
david said:
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.
My options parser depends only on the standard library. The alternatives you
suggest may not be available on the OP's platform.
 
F

Flash Gordon

Malcolm McLean wrote, On 20/02/08 20:46:
My options parser depends only on the standard library. The alternatives
you suggest may not be available on the OP's platform.

The OP stated he is using MacOS X and that is a Unix variant and
therefore definitely has getopt as I stated. I'm not certain about
getopt_long (hence the reference to GNU groups) but it is highly likely
and if not the sources *are* easily available in standard C in a number
of places.

Far better to use a function that will give the same behaviour as most
the other applications on the target box and is used so heavily that it
is extensively tested than to use a lightly used package that is less
likely to produce identical behaviour.
 
D

Default User

david said:
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?

Did you trying searching for: comp.lang.c faq ?




Brian
 
C

CBFalconer

david said:
Code:
#include <stdio.h>
#include <strings.h>
#include <stdlib.h>

int main (int argc, char const *argv[]) {
if (argc == 1)

Use "<=" here. Who says argc can't be zero?
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?

Look up the strcmp function.
2) How can I get first char of the first parameter? (It's kinda hard
to understand how to write this)

if (argv[1][0] == '-') ...

Note use of ' for chars, not " for strings.
 
C

CBFalconer

david said:
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)

Some useful links on quoting:
<http://www.xs4all.nl/~wijnands/nnq/nquote.html>
<http://www.complang.tuwien.ac.at/anton/mail-news-errors.html>
<http://www.netmeister.org/news/learn2quote2.html>
<http://www.star-one.org.uk/computer/format.htm>

Usenet is a 'best efforts' protocol. There is no guarantee that
your reader can ever read any other message in a thread. Thus you
should always quote enough for your message to stand entirely by
itself.
 
C

CBFalconer

Flash said:
Malcolm McLean wrote, On 20/02/08 18:00:

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.

No he shouldn't. He should learn to do these basic operations long
before using those.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top