-4

OMG, I know this should be extremely simple and I have tried everything I have researched but nothing works. I am missing something simple, please help. I just want to declare a string and then later compare it to another string.

I want to declare a key of something like 9 characters. Then I want to compare this string to another one submitted later. Below is what I have and no matter what I change I get errors from incompatible types to missing token.

char key[] ="kjherres";
char f[];

F="kjherres";    

if (key==f) {
 //run my code
}

I have also tried (strcmp) to no avail. What am I missing? Please help.

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
nathan
  • 477
  • 1
  • 4
  • 8
  • 1
    And your sample doesn't even compile. – Ed S. Apr 29 '14 at 02:38
  • 2
    "OMG, I know this should be extremely simple and I have tried everything" No you haven't. You haven't even typed "string compare in C" into Google... – John3136 Apr 29 '14 at 02:42
  • OK, I figured it out but I guess my issue is it is not working properly. if (strcmp(key,f)==0) {}. But I get nothing. If I use =!0 not zero it does not compare the two why is it not comparing the 2? I am missing something stupid I know. I have not programmed C in over 15 years, please forgive the ignorance here on primitive code. – nathan Apr 29 '14 at 02:59

2 Answers2

1

The way you compare strings is with the strcmp function.

if ( strcmp(key,f) == 0 ) {
    /* strings are the same */
}

You should have a good book or online tutorial to learning C. If you're coming from PHP or some other high-level language, you have a LOT to learn and if you don't do it right, you're asking for big problems. Strings are not as trivial in C as they are in PHP etc.

Also, C is case-sensitive. F and f are different names.

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
  • 1
    People ought be taught entry-level (but good-quality) C in public school, along with math. Come to think of it, I think I posted that on twitter earlier this year. – Tom Pace Apr 29 '14 at 02:43
-1

OK, this was a stupid thing I missed as always. Sometimes my mind is going faster than my fingers.

What the problem was is that I assigned f from a url param but I was only reading the first char of the param. Forgive me for the ignorance, it can happen often when going too fast.

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
nathan
  • 477
  • 1
  • 4
  • 8