为什么此代码从 'char' 错误中获取对 'char *' 的赋值?

Why is this code getting an assignment to 'char *' from 'char' error?

编译时出现错误。

incompatible integer to pointer conversion assigning to 'string'
      (aka 'char *') from 'char'; take the address with &

我的代码:

#include<stdio.h>
#include<cs50.h>
#include<string.h>

int pallin(string A);
int main(void)
{
  printf("Enter the string to analyze\n");
  string S[10];
  S = GetString();
  int flag = pallin(S);
  if(flag == 0)
  {
    printf("Invalid input\n");
  }
  else if (flag == 1)
  {
    printf("Yes, the input is a pallindrome\n");
  }
  else{
    printf("The input is not a pallindrome\n");
  }
}

int pallin(string A)
{
  int flag;
  int n = strlen(A);
  if(n<=1)
  {
    return 0;
  }
  else 
  {string B[10];int i = 0;

         while(A[i]!="[=13=]")
         {
         B[i]=A[n-i-1];  //Getting error here.
         i++;
         }

      for(int j = 0; j < n; j++)
      {
          if(B[j]!=A[j])
          {
              flag = 2;
          }
          else
          {
              flag = 1;
          }
      }
      return flag;
  }
}

我不喜欢 CS50 typedef char *string; — 它的帮助不够,而且确实造成了太多混乱。您不能使用 string.

声明字符数组

此代码有效:

#include <stdio.h>
#include <cs50.h>
#include <string.h>

int palin(string A);

int main(void)
{
    printf("Enter the string to analyze\n");
    string S = GetString();
    int flag = palin(S);
    if (flag == 0)
    {
        printf("Invalid input\n");
    }
    else if (flag == 1)
    {
        printf("Yes, the input is a palindrome\n");
    }
    else
    {
        printf("The input is not a palindrome\n");
    }
}

int palin(string A)
{
    int flag;
    int n = strlen(A);
    if (n <= 1)
    {
        return 0;
    }
    else
    {
        char B[100];
        int i = 0;

        //while (A[i] != "[=10=]")
        while (A[i] != '[=10=]')
        {
            B[i] = A[n - i - 1]; // Getting error here.
            i++;
        }

        for (int j = 0; j < n; j++)
        {
            if (B[j] != A[j])
            {
                flag = 2;
            }
            else
            {
                flag = 1;
            }
        }
        return flag;
    }
}

main() 中对 string S = GetString(); 进行了更改; char B[100];palin() 中;重新拼写 'palindrome';使用 '[=18=]' 代替 "[=19=]" (这也有其他问题;在这种情况下它与 "" 相同,这不是您比较字符串的方式(在一般意义上也是如此作为 CS50 意义)——如果你想比较字符串,你需要 strcmp(),但在这种情况下你不需要)。

它不会释放分配的字符串。它确实产生了正确的答案(程序名称 pa19):

$ pa19
Enter the string to analyze
amanaplanacanalpanama
Yes, the input is a palindrome
$ pa19
Enter the string to analyze
abcde
The input is not a palindrome
$ pa19
Enter the string to analyze

Invalid input
$