Thread: question about function strcmp() in C

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    41

    question about function strcmp() in C

    There is partial code in C
    Code:
    typedef struct message {
        int messageId;
        char *messageText;
        struct message *next;
    }message;
    .....
    .....
    .....
    /* Get a node before a node */
    message *nodeBefore ( message *text ) {
        message *tem;
        tem = head;
        /* If there are only 2 nodes ( a and b ),then nodeBeforeByText of a is returning to a */
    184:if ( strcmp(tem->messageText,text) == 0)
               return tem;
           else {
               while ( tem  != NULL ) {	    
    188:         if ( strcmp(tem->next->messageText,text) == 0 )            
                        break;        
                 else
                        tem = tem->next;
              }
           }
           return tem;   
    }
    When I compiled there were messages :
    Code:
    q2.c:184: warning: passing arg 2 of `strcmp' from incompatible pointer type
    q2.c:188: warning: passing arg 2 of `strcmp' from incompatible pointer type
    After looking up the reference for strcmp() in library, I still dont understand why there are messages here.Can some body explain to me.
    Last edited by thungmail; 03-12-2008 at 05:28 PM.

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    'text' is a pointer to a stuct. You want to pass in the pointer to the string within the struct.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    41
    hi there
    yeah yeah i forgot it. I should delare :
    Code:
    message *nodeBefore ( char *text ) {
    ........
    ........
    ........
    }
    Is it correct ?

  4. #4
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Maybe take a closer look at the definition of struct message, a pointer of which is your variable named 'text', and find a string from there to give to strcmp.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The correct answer depends on the call to
    Code:
    message *nodeBefore ( message *text )
    - does it call with a message or a char *?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I'd suggest you stop calling everything message, it's confusing

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  2. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  3. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM