简体   繁体   English

编译时获取结构指针警告的 -Wincompatible-pointer-types

[英]Getting -Wincompatible-pointer-types for struct pointer warning when compiling

I'm following along with this tutorial to learn about external chaining in Hashtables in C.我正在跟随教程学习 C 中哈希表中的外部链接。 I find that I get the following error when compiling with gcc --std=c99 :我发现使用gcc --std=c99编译时出现以下错误:

warning: assignment to "person *" {aka 'struct <anonymous> *'} from incompatible pointer type 'struct person *' [-Wincompatible-pointer-types]
101 | tmp = tmp->next;

warning: assignment to "person *" {aka 'struct <anonymous> *'} from incompatible pointer type 'struct person *' [-Wincompatible-pointer-types]
112 | p->next = hash_table[index];

warning: assignment to "person *" {aka 'struct <anonymous> *'} from incompatible pointer type 'struct person *' [-Wincompatible-pointer-types]
121 | tmp = tmp->next;

warning: assignment to "person *" {aka 'struct <anonymous> *'} from incompatible pointer type 'struct person *' [-Wincompatible-pointer-types]
132 | tmp = tmp->next;

warning: assignment to "person *" {aka 'struct <anonymous> *'} from incompatible pointer type 'struct person *' [-Wincompatible-pointer-types]
136 | hash_table[index] = tmp->nxt;

I find that this occurs only with the next pointer for the person struct:我发现这只发生在 person 结构的next指针中:

typedef struct {
   char name[TABLE_SIZE];
   int age;
   //...
   struct person *next;
} person;

For functions like these:对于这样的功能:

void print_table(){
   for (int i=0; i < TABLE_SIZE; i++0 {
       if (hash_table[i] == NULL) {
           printf("\t%i\t---n\n",i);
       }
       else {
            printf("\t%i\t",i);
            person *tmp = hash_table[i];
            while (tmp != NULL) {
                printf("%s -", tmp->name);
                tmp = tmp->next;
            }
            printf("\n");
       }
   }
}

bool hash_table_insert(person *p) {             
    if (p == NULL) return false;                
    int index = hash(p->name);
    p->next = hash_table[index];
    hash_table[index] = p;
    return true; 
}

The hashtable's output is correct so clearly it works fine but it gets these warnings.哈希表的 output 是正确的,所以很明显它工作正常,但它会收到这些警告。 I did not get these warnings until implementing the chaining technique.在实施链接技术之前,我没有收到这些警告。 I'm inclined to believe it has something to do with the addition of struct person *next;我倾向于相信这与添加struct person *next;有关系。 , or at least it's usage. ,或者至少是它的用法。 Any ideas?有任何想法吗?

This typedef declaration这个 typedef 声明

typedef struct {
   char name[TABLE_SIZE];
   int age;
   //...
   struct person *next;
} person;

declares an unnamed structure with the typedef name person , and this record within the unnamed structure使用 typedef 名称person声明一个未命名结构,并且该记录位于未命名结构中

   struct person *next;

introduces an incomplete type specifier struct person .引入了一个不完整的类型说明符struct person

Thus person and struct person are two different type specifiers.因此personstruct person是两个不同的类型说明符。

You could, for example例如,您可以

typedef struct person person;

struct person {
   char name[TABLE_SIZE];
   int age;
   //...
   struct person *next;
};

or或者

typedef struct person {
   char name[TABLE_SIZE];
   int age;
   //...
   struct person *next;
} person;

An anonymous struct is a struct without a tag .匿名结构是没有标签的结构。 Give your struct a tag and then use struct person everywhere.给你的结构一个标签,然后在任何地方使用struct person

And don't bother with typedefs for structs.并且不要为结构体的 typedefs 烦恼。 All they do is save you from typing struct in a few places, like declarations.他们所做的只是让你不用在几个地方输入struct ,比如声明。

struct person {
   char name[TABLE_SIZE];
   int age;
   //...
   struct person *next;
};
...
struct person *tmp = hash_table[i];

On another note, functions taking no arguments should be declared with a (void) argument list, otherwise they indicate an unspecified but fixed argument list.另一方面,不采用 arguments 的函数应该用(void)参数列表声明,否则它们表示未指定但固定的参数列表。 In particular, in C, something like特别是,在 C 中,类似

int foo();

is not a prototype!不是原型! It's an old-style (K&R C) function declaration from the age when C did not have the void keyword.这是 C 没有void关键字时的旧式 (K&R C) function 声明。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 警告:C 中不兼容的指针类型 [-Wincompatible-pointer-types] - warning: incompatible pointer types [-Wincompatible-pointer-types] in C -Wincompatible-pointer-types和enum - -Wincompatible-pointer-types and enum 警告:从不兼容的指针类型返回 [-Wincompatible-pointer-types]| - warning: return from incompatible pointer type [-Wincompatible-pointer-types]| 从不兼容的指针类型 [-Wincompatible-pointer-types] 获取传递“pthread_create”参数 3 的警告 - Getting warning of passing argument 3 of ‘pthread_create’ from incompatible pointer type [-Wincompatible-pointer-types] C [-Wincompatible-pointer-types] 如何转换 - C [-Wincompatible-pointer-types] how to cast C中的函数指针:警告:来自不兼容指针类型[-Wincompatible-pointer-types]的赋值 - Function pointers in C: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types] RecursiveFree函数-警告:从不兼容的指针类型[-Wincompatible-pointer-types]进行初始化 - RecursiveFree Function - Warning: initialization from incompatible pointer type [-Wincompatible-pointer-types] 警告:从不兼容的指针类型 [-Wincompatible-pointer-types]| 传递 'transform_labels' 的参数 2 | - Warning: passing argument 2 of 'transform_labels' from incompatible pointer type [-Wincompatible-pointer-types]| 警告:从不兼容的指针类型 [-Wincompatible-pointer-types] 传递“接受”的参数 2 - warning: passing argument 2 of ‘accept’ from incompatible pointer type [-Wincompatible-pointer-types] 来自不兼容指针类型的赋值[Wincompatible-pointer-types] - Assignment from incompatible pointer type[Wincompatible-pointer-types]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM