0

I need some assistance with the strcmp function. I am trying to compare a const char word with a char array.But for whatever reason, strcmp is not comparing the two types.Here is what I have:

typedef struct Node {    
    char word[LENGTH+1];  
    struct Node *Next;    
}
Node;


for (NodePointer=hashtable->table[hashval];NodePointer !=NULL;NodePointer=NodePointer->Next)
    {
        i=0;
        i=strcmp(word,NodePointer->word); 

        if (i==0) 
          return true;
    }

    return false;
}

does anyone have any idea on what maybe the problem? Am I suppose to null terminate each beforehand?

Scooter
  • 6,802
  • 8
  • 41
  • 64
user2014904
  • 81
  • 1
  • 8
  • 4
    `strcmp` relies on a terminating null char to signal the end of a string, so if one of your strings doesn't end with a null char, you'll certainly have a problem. – Charles Salvia Mar 09 '13 at 03:26
  • We'll probably need more than this, but yes you should nul terminate anything you pass to strcmp. – user7116 Mar 09 '13 at 03:26
  • 4
    Well, I guarantee `strcmp` works on *valid* inputs, so.. –  Mar 09 '13 at 03:26
  • Ok, and this will totally come across as a dumb question, but how would I null terminate an array within a structure? – user2014904 Mar 09 '13 at 03:28
  • Add the line where `word` is assigned – uba Mar 09 '13 at 03:29
  • I guess following link might help you http://stackoverflow.com/questions/1330550/c-compare-char-array-with-string – suraj_fale Mar 09 '13 at 03:32
  • @uba I just added the structure where I create word array. – user2014904 Mar 09 '13 at 03:33
  • If you know the length of your strings, but they're not null terminated, use memcmp instead. But make sure that the size you give it is the *minimum* of both lengths. – Dave Mar 09 '13 at 03:41
  • @user2014904 How are you **assigning** the values? Give an example of assigning (are you reading it from stdin?) `word` or `Node->word` – uba Mar 09 '13 at 03:56
  • I am assigning it like this: – user2014904 Mar 09 '13 at 04:20
  • I am strcpying Node->word from a buffer array. buffer is read from a file, and I'm using fscanf as the tool to read the data from the file into the buffer array... strcpy(node-word,buffer) – user2014904 Mar 09 '13 at 04:22
  • Please give us a minimal, **compilable** testcase so we can diagnose your problem. Minimal means "Using only the bare essentials required to reproduce your problem". Compilable means "Using enough code for us to compile and reproduce the problem on our own systems **without filling in blanks, guessing or fixing syntax errors**. Also, if there are any warnings issued by the compiler, please provide them. – autistic Mar 09 '13 at 04:24
  • I figured it out guys...thank you anyways! – user2014904 Mar 09 '13 at 05:17
  • Remove the i=0 line, the effect of that are forgotten anyway in the next line. – Anders Lindén Mar 09 '13 at 10:23
  • [What is a null-terminated string?](http://stackoverflow.com/q/2037209/995714) – phuclv Mar 27 '17 at 02:38

1 Answers1

1
  1. make sure word is terminated by '\0'.
  2. you can considering using strncmp

    strncmp(word, NodePointer->word, LENGTH)

zzk
  • 1,347
  • 9
  • 15