Char to Char* Error Message

2019-09-20 14:12发布

问题:

Everytime I try to compile my program, I get the Char to Char* compile error... I am doing this in C. This is my code:

int my_strcmp(char s1[], char s2[]) {
  int i;
  for (i=0; i != '\0'; i++)
    if (my_strcmp(s1[i], s2[i]) == 1)
      return 1;
    else if (my_strcmp(s1[i], s2[i]) == -1)
      return -1;
    else
      return 0;

回答1:

  • s1 has type char *
  • Therefore, s1[i] has type char
  • my_strcmp() expects two char * variables as arguments.
  • You pass s1[i] (which we just said is a char) as one of the arguments.
  • char and char * are different types.


回答2:

Your function my_strcmp is declared to use array of chars as input arguments (char* or equivalently char[]), whereas in line if (my_strcmp(s1[i], s2[i]) == 1) this function is invoked with characters s1[i] and s2[i]. Hence the compiler complains about char to char* conversion.