-
char* Problem
This is my first post, so please forgive me if I haven't followed the guidelines correctly!
I am writing a C function which returns an integer and accepts a char* as an argument. The function is an if block to compare the text in the char* value (or should I say the location to which it points!) and set the integer to a value depending on the text value:
Code:
int function_name(char* input)
{
int outputNum;
if (input == "TextValue") outputNum = 0;
else if (input == "OtherValue") outputNum = 1;
else if (input == "OneMore") outputNum = 2;
else outputNum = 3;
return outputNum;
}
This isn't the actual function: I cannot post the actual function as it is part of a proprietary package I am writing!
The problem I am having is that whatever text value is passed in, the int is set to 3 (the else value), even if the char* is, say, "TextValue". When invoking the function, I do as:
function_name(input); where input is of type char*.
Am I missing something very basic or is there something else to it? I have thought about the strstr() function in string.h, but I would like to avoid that if, at all, possible.
-
In terms of posting, you should read the sticky "Posting Code? Read This First!" or similar.
In terms of your question, you should read the manual on strcmp.
-
Apologies! I shall remember for next time about the code button!
Thank you for the strcmp() function - does exactly what I need it to do!