简体   繁体   English

来自不兼容的指针类型C的许多分配

[英]Many assignments from incompatible pointer type C

I'm pretty new to C, and I don't have a very good grasp of pointers. 我对C很陌生,并且对指针没有很好的了解。 I'm trying to build a hash table. 我正在尝试建立一个哈希表。 Here's what I have so far. 到目前为止,这就是我所拥有的。

I've been cobbling this together from several sources, and I've lost any idea I have of what the pointers are doing. 我一直在从几个方面来解决这个问题,而我对指针的作用一无所知。 If anyone could even give me a hint as to where my problem is I'd be very grateful. 如果有人能给我提示我的问题在哪里,我将不胜感激。

Header File 头文件

 typedef struct {
   char * word;
   char * defn;
   struct entry *next;
 } entry;


 typedef struct {
   int size;
   struct entry **table;
 } hashTable;

 typedef hashTable * Dictionary;

Code

 #include "hashP.h"
 #include <stdlib.h>
 #include <string.h>
 #include <limits.h>


 Dictionary create(int initial_capacity, int delta_capacity){
     Dictionary new_table;
     int i;

     if ((new_table = malloc(sizeof(Dictionary))) == NULL){
         return NULL;
     }

     if ((new_table->table = malloc(sizeof(entry *) * initial_capacity)) == NULL){
         return NULL;
     }

     for(i=0; i < initial_capacity; i++){
         new_table->table[i] = NULL;
     }
     return new_table;
 }


 /* Insert a key-value pair into a hash table. */
 void insertEntry(Dictionary table, char *index, char *value) {
     int bin = 0;
     entry *newpair = NULL;
     entry *next = NULL;
     entry *last = NULL;
     unsigned long int hashval;
     int i = 0;
     char *word = index;
     char *defn = value;

     /* Convert our string to an integer */
     while( hashval < ULONG_MAX && i < strlen(word) ) {
         hashval = hashval << 8;
         hashval += word[i];
         i++;
     }

     bin = hashval % table->size;

     next = table->table[bin];


     while( next != NULL && next->word != NULL && strcmp(word, next->word ) > 0 ) {
         last = next;
         next = next->next;
     }

     /* There's already a pair.  Let's replace that string. */
     if( next != NULL && next->word != NULL && strcmp( word, next->word ) == 0 ) {

         free( next->defn );
         next->defn = strdup(defn);

     /* Nope, could't find it.  Time to grow a pair. */
     } else {

         if( ( newpair = malloc( sizeof(entry) ) ) == NULL ) {
             return NULL;
         }

         if( ( newpair->word = strdup(word) ) == NULL ) {
             return NULL;
         }

         if( ( newpair->defn = strdup(defn) ) == NULL ) {
             return NULL;
         }

         newpair->next = NULL;

         /* We're at the start of the linked list in this bin. */
         if( next == table->table[ bin ] ) {
             newpair->next = next;
             table->table[bin] = newpair;

         /* We're at the end of the linked list in this bin. */
         } else if ( next == NULL ) {
             last->next = newpair;

         /* We're in the middle of the list. */
         } else  {
             newpair->next = next;
             last->next = newpair;
         }
     }
 }

Sorry for the huge wall of text. 对不起,那巨大的文字墙。 It gives me an "Assignment from incompatible pointer type" every time I use "next". 每次我使用“ next”时,都会给我一个“来自不兼容指针类型的赋值”。

If you declare your struct as: 如果您将结构声明为:

struct entry { ... };

Then you should use it as 然后,您应该将其用作

struct entry* next;

If you declare it as: 如果您声明为:

typedef struct { ... } entry;

Then you should use it as 然后,您应该将其用作

entry* next;

In this case struct entry* next will still compile (as in your case), but will refer to an incomplete type, which is not the entry that you defined. 在这种情况下, struct entry* next仍将编译(与您的情况相同),但将引用不完整的类型,这不是您定义的entry Assigning from something that is entry* to something that is struct entry* will, therefore, give you an error. 因此,从entry*struct entry*分配会给您带来错误。

To fix your problem just replace all occurrences of struct entry with entry . 要解决您的问题,只需将所有出现的struct entry替换为entry

UPDATE: it will not work, because by the time you define entry* next entry itself is not defined yet. 更新:它将不起作用,因为在您定义entry* next尚未定义entry* next entry You can fix it like this, for example: 您可以像这样修复它,例如:

 typedef struct entry_t {
   char * word;
   char * defn;
   struct entry_t *next;
 } entry;

Beside other problems, Dictionary is not defined. 除了其他问题,未定义Dictionary From how you use it, I am guessing it is a typedef for a pointer to a hashTable . 从您的使用方式来看,我猜测它是用于hashTable指针的typedef The allocation is wrong, you allocate the size for a pointer, not for the hashTable structure. 分配是错误的,您为指针分配了大小,而不是为hashTable结构分配了大小。 You should write: 您应该写:

Dictionary new_table = malloc(sizeof(*new_table));

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM