【问题标题】:Incompatible Pointer Type Error不兼容的指针类型错误
【发布时间】:2010-10-25 02:16:53
【问题描述】:

大家好, 我的代码中出现编译错误,我不知道该怎么做。 这是块:

#include <stdio.h>
#include <string.h>    
/*
 * Function to return index at which team ID input is stored
 */
int getIndex(char* id, char* idList[][50]) {
    int k;
    for (k=0; k<50; k++) {
        if (strcmp(id,idList[k])==0) {
            return k;
        }
    }
    printf("Error in getIndex function.\n");
    return -1;
}

错误提示

Passing argument 2 of 'strcmp' from incompatible pointer type

错误发生在块中的第 8 行代码(if 语句)。

编辑 (代表迈克尔在这里发布,因为他还不能编辑他的帖子)

我会具体说明我想做什么,因为我在这方面做得并不好。

我希望 id[] 应该是一个最多 50 个字符的数组,以与 idList[][] 进行比较,idList[][] 是一个字符串数组(最多 50 个字符串,每个最多 50 个字符)。

我把代码改成了这个,

/*
 * Function to return index at which team ID input is stored
 */
int getIndex(char* id[], char* idList[][50]) {
    int k;
    for (k=0; k<50; k++) {
        if (strcmp(id[],idList[k][])==0) {
            return k;
        }
    }
    printf("Error in getIndex function.\n");
    return -1;
}

但出现错误提示:

Expected expression ']' before token

【问题讨论】:

    标签: c pointers arguments


    【解决方案1】:

    表达式idList[k]char* [50] 对象,而不是char* 对象。您可能打算创建签名char idList[][50]char* idList[50],或者您可能打算提供第二个索引(如idList[k][j])。这就是错误消息的含义。显然,您最了解该功能,因此您最有能力确切地知道您所指的这些选项中的哪一个。

    编辑
    根据此更新信息,您可能想要的是:

    int getIndex(const char* id, const char* id_list[], int id_list_length) {
        for (int i = 0; i < id_list_length; i++) {
            if (!strcmp(id, id_list[i])) {
                return i;
            }
        }
        printf("Error in getIndex function; ID \"%s\" not found.\n", id);
        return -1;
    }
    

    首先,请注意我使用<b>const</b> char* 而不是char*。这是一个改进,它告诉编译器字符串的内容不会被修改。其次,列表的大小在参数中给出,而不是硬编码到函数签名中。最后,在签名中使用括号(即[])要少得多(通常在 C 和 C++ 中,通常更常见的是在签名中看到指针,特别是考虑到数组实际上只不过是指向重复的指针数据)。您可以在创建数组的地方强制执行长度要求,但是,通常更常见的是允许长度是动态的并自动计算。这是一个使用示例:

    const char* id_list[] = { "Alpha", "Bravo", "Charlie" };
    int id_list_length = 3;
    int zero = getIndex("Alpha", id_list, id_list_length);
    int one = getIndex("Bravo", id_list, id_list_length);
    int two = getIndex("Charlie", id_list, id_list_length);
    int negative_one = getIndex("Not in there", id_list, id_list_length);
    

    您也可以考虑修改它以使用 NULL 作为列表的终止字符:

    int getIndex(const char* id, const char* null_terminated_id_list[]) {
        for (int i = 0; null_terminated_id_list[i] != NULL; i++) {
            if (!strcmp(id, null_terminated_id_list[i])) {
                return i;
            }
        }
        printf("Error in getIndex function; ID \"%s\" not found.\n", id);
        return -1;
    }
    

    那么你甚至不需要记录列表的长度,你可以这样写:

    const char* id_list[] = { "Alpha", "Bravo", "Charlie", NULL };
    int zero = getIndex("Alpha", id_list);
    int one = getIndex("Bravo", id_list);
    int two = getIndex("Charlie", id_list);
    int negative_one = getIndex("Not in there", id_list);
    

    【讨论】:

    • 我试图比较 id[] (我省略了代码中的括号)和 idList[k][] (我不确定 k 是否在右括号中)。我想将 'string' id 与 idList 中的所有 'strings' 进行比较(每个字符串包含 50 个 50 个字符)。
    • @Michael:如果 idList 是 50 个字符串的列表,那么它可以声明为 char *idList[50],记住编译器不会强制它确实是 50。你已经声明idList 作为char* 的二维数组,但您似乎想要char* 的数组。
    【解决方案2】:

    如果您的 idList 参数应该表示一个线性 ID 数组,那么它应该声明为 char idList[][50]char *idList[],这取决于数组本身是否实际提供字符串内存(前者)或字符串内存分配在别处(后者)。您很可能需要前一种变体。

    你现在所拥有的 - char *idList[][50] 看起来像是两者的奇怪混合体,在你的意图上下文中毫无意义,我理解它的方式。

    【讨论】:

      【解决方案3】:

      在 VS 中,我收到此错误:

      错误 C2664:“strcmp”:无法将参数 2 从“char *[50]”转换为“const char *”

      问题在于idList[k] 是指针数组,而不是字符数组。

      在 C 中,字符串可以是 char*char[],但不是 char*[]char*[] 将是一个字符串数组,这意味着 char*[][](这是您的函数当前采用的)是一个字符串数组数组...

      您需要更改您的函数签名(更改为 char idList[][50]char* idList[50]),或者添加第二个索引(例如 idList[k][j])以使签名正确匹配。

      你有一个字符串数组,还是一个字符串数组?

      • 如果您有一个字符串数组,请修复函数签名:char* idList[50]
      • 如果您有一个由字符串组成的数组,则需要更改函数的主体,可能还有签名,以传递每个内部数组的大小:

      第二个选项的示例代码...

      int getIndex(char* id, char* idList[][50], size_t idListSizes[50]) {
        int k, l;
        for (k=0; k<50; k++) {
          for (l=0; l<idListSizes[k]; l++) {
            if (strcmp(id,idList[k][l])==0) {
              /* somehow return both k and l */
      

      编辑

      在看到您的编辑(您将其发布为您的问题的答案 :))后,这就是您想要的:

      int getIndex(char id[50], char idList[50][50]) {
        int k;
        for (k=0; k<50; k++) {
          if (strcmp(id,idList[k])==0) {
            return k;
          }
        }
        printf("Error in getIndex function.\n");
        return -1;
      }
      

      这里的关键是您不必使用char* 来表示字符串。 char[] 是一回事。

      还要注意,在某些情况下,C 不会强制执行大小。使用我刚刚发布的代码,它仍然可以编译:

      char blah[70];
      char blee[14][50];
      return getIndex(blah, blee);
      

      虽然这不会:

      char blah[70];
      char blee[14][27];
      return getIndex(blah, blee);
      

      编辑

      最后一件事,你需要你的字符串大小为 51,因为终止 null。例如:

      char test[] = "test"; /* This is actually an array of size 5, not 4 */
      char alt[] = { 't', 'e', 's', 't', '\0' }; /* same content as test... */
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-07
        • 2010-10-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-04
        相关资源
        最近更新 更多