LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   [C] three questions: (https://www.linuxquestions.org/questions/programming-9/%5Bc%5D-three-questions-106329/)

wuck 10-20-2003 11:49 AM

[C] three questions:
 
one

Code:

for ( x = 1 ; x < argc ; x++ )
                {
                        printf ("%i %s\n", x, argv[x]);

                        if (argv[x] == "joe")
                                printf ("You said joe\n");

                }

If I put this in my main() function, compile it, and say:

$ ./compiledbinary joe

It does not print back 'You said joe'. However, it does print

'1 joe'

Which means that printf 'can read' argv[x], but it seems == 'can't' compare argv[x] with a static string. If I make a variable called foo and put "joe" (char *foo = "joe";) in it, it _does_ work.

:confused:

two

Code:

char a;

switch (a)
{
        case 'A':
        case 'B':
                CatBillGatesDevNull ();
                break;
        case 'C':
                Joseph ();
                break;
        default:
                Ko ();
                break;
}

This is perfectly legal, but is it possible to switch strings? I think so, but I wouldn't know how. It'd be odd that just for strings, we'd be stuck with a whole lot of if statements :)

three

I'd like to write KDE apps with Kdevelop in C. What approach is there?

patanaheen 10-20-2003 11:59 AM

one:

the string dont get compared the way you are trying to do ... strings are stored in C as arrays of charaters that are terminated by a NULL ('\0') character... therefore to get what you want do this strcmp(argv,"joe") also add string.h in your header files... for the case of char *foo u are actually comparing two characters which in your case are equal... *foo=argv[x] ...

two:

again string comparision are not inbuilt into C therefore u cannot switch on them use strcmp library function and if statement...

three:

just use one of the wizrds and generate a skeleton program then play with it ... u will get the idea ...

tcaptain 10-20-2003 12:02 PM

for one:

That's because I don't think you can compare strings with '=='
you need to use strcmp (IIRC)

wuck 10-20-2003 12:13 PM

Thank you! I was wondering about a strcmp function somewhere ...

/* looks at wrist */ 10 minutes sweet reply time!

wuck 10-20-2003 12:24 PM

/* please never mind */


All times are GMT -5. The time now is 10:51 AM.