1
int function(char * WordList[], int nSize, char * param_str)

{
  int i = 0;
    int bFlag = 0;
    
    while(i < nSize && !bFlag)
    {
        if(WordList[i] == param_str){
            return bFlag = 1;
        }
        i++;
    }
    return bFlag;

i tried using strcmp and made it work [if(strcmp(WordList[i], param_str) == 0)] but im wondering whats wrong with my current code.

1 Answers1

0

WordList[i] is a pointer

param_str is another pointer.

WordList[i] == param_str tests if the 2 pointers point to the same location.

What these pointers reference is not considered. WordList[i] == param_str is not a string compare, just an address compare.


strcmp(a,b) compares the data at locations a and b.

To do the same, without strcmp(), form your own ...

int my_strcmp(const char *a, const char *b) {
  // Pseudo code
  while what `a` points to is the same as what `b` points to and `*a` is not 0
    advance both pointers
  
  return the sign of the difference of `*a` and `*b`.
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256