Thread: char* Problem

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    2

    Question 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.
    Last edited by Salem; 04-24-2010 at 02:13 PM. Reason: Added [code][/code] tags - learn to use them yourself

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    2
    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

Tags for this Thread