来自不兼容指针类型C

时间:2015-12-02 23:45:44

标签: c pointers

我对C很陌生,而且我对指针的掌握并不是很好。我正在尝试构建一个哈希表。这是我到目前为止所拥有的。

我从几个来源一直在拼凑这个,我已经不知道我有什么指针在做什么。如果有人甚至可以给我一个暗示我的问题在哪里,我将非常感激。

标头文件

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


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

 typedef hashTable * Dictionary;

代码

 #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;
         }
     }
 }

抱歉,这是一面巨大的文字墙。每次我使用“next”时,它都会给我一个“来自不兼容指针类型的赋值”。

2 个答案:

答案 0 :(得分:4)

如果您将结构声明为:

struct entry { ... };

然后你应该用它作为

struct entry* next;

如果您将其声明为:

typedef struct { ... } entry;

然后你应该用它作为

entry* next;

在这种情况下,struct entry* next仍会进行编译(如您的情况),但会引用一个不完整的类型,即您定义的entry。因此,从entry*的内容分配到struct entry*的内容会给您一个错误。

要解决您的问题,只需将所有struct entry替换为entry

更新:它不起作用,因为当你定义entry* next entry时,它本身尚未定义。您可以像这样修复它,例如:

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

答案 1 :(得分:0)

除了其他问题, long GetItemData(long Index); double* valuePtr = (double*) GetItemData(1); double value = *valuePtr; 未定义。从你如何使用它,我猜测它是Dictionary指向typedef的指针。分配错误,您为指针分配大小,而不是hashTable结构。你应该写:

hashTable