#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    8
    Rep Power
    0

    Angry strcmp causes seg fault


    Howdy,
    The following portion of code won't admit that ptr->Name and S are equal (even though they are both char * and the printf says they say the same thing), but if I use strcmp (or stricmp) instead, I get a segmentation fault.
    Anyone with any ideas?:confused:
    PLEASE?

    printf("\nS: %s, ptr->Name: %s, x: %d\n", S, ptr->Name, x);

    if (ptr->Name == S)
    return x;


    Bearette
  2. #2
  3. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,110
    Rep Power
    1803
    char* is a pointer. The pointer S is not the same as the pointer ptr->Name. (they may however point to identical content in separate locations).

    C Style strings (Nul terminated character arrays) are not a built in data type in C, simply a representation convention. The language cannot compare string content in this way. You need to use the string library function strcmp() (as the title of your message suggests oddly enough!).

    Clifford
    Last edited by clifford; August 18th, 2003 at 11:14 AM.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    8
    Rep Power
    0
    Which is why I had written "but if I use strcmp (or stricmp) instead, I get a segmentation fault" ... you must have missed that bit! :D
    That is the whole problem with the code. I have fiddled in all sorts of ways, but strcmp continues to give me a segmentation fault, and I don't know why.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    Originally posted by Bearette
    Which is why I had written "but if I use strcmp (or stricmp) instead, I get a segmentation fault" ... you must have missed that bit! :D
    That is the whole problem with the code. I have fiddled in all sorts of ways, but strcmp continues to give me a segmentation fault, and I don't know why.
    You're probably trying to feed strcmp an uninitialized pointer. The segmentation fault indicates that you are trying to access memory that doesn't belong to you. Verify that your pointers are pointing to actual strings that you have created within your program and not to the system's vector table or other sensitive areas.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    8
    Rep Power
    0
    Isn't that what
    printf("\nS: %s, ptr->Name: %s, x: %d\n", S, ptr->Name, x);
    would do? :confused:
  10. #6
  11. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    no, by 'verify' he means initialize your pointers to explicity point to allocated memory or NULL. some code...
    Code:
    char *ptr;               //bad
    char *good_ptr = NULL;          //good
    char *g00d_ptr = new char[10];
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    8
    Rep Power
    0
    Oh, I see. They were initialised (function newEntry() set all the pointers to NULL), but I had overlooked what happened if ptr->Name == NULL, so that has just solved it for me and the code now reads -
    if ((ptr->Name != NULL) && (strcmp(ptr->Name, S) == 0))

    etc.

    Thanks very much for the redirection of thinking! :D
    Now I can actually get on with something new!!!

IMN logo majestic logo threadwatch logo seochat tools logo