0

这给我的程序带来了很多问题。为什么当我创建一个新的结构化指针数组时它们都等于'\0'?我检查了它们是否位于数组的末尾,if(table_p -> buckets_array[i] == '\0'){ printf("ask this \n") ; }并且对于数组的每个成员都是如此。我检查错了吗?不应该只有最后一个成员有\0吗?

typedef struct data_{
  char *key;
  void *data;
  struct data_ *next;
}data_el;

typedef struct hash_table_ {
  void **order;
  int *number_next_calls;
  int *number_buckets;
  int *buckets_size;
  int *worst;
  int *total;
  float *average;
  int (*hash_func)(char *);
  int (*comp_func)(void*, void*);
  data_el **buckets_array;
} hash_table, *Phash_table;

/*Create buckets array*/
table_p -> buckets_array = (data_el **)malloc(sizeof(data_el *)*(size+1));
table_p -> buckets_size = (int *)malloc(sizeof(int)*(size+1));

/*Setting order array*/
  table_p -> order = NULL;

/*Setting inital condictions*/
table_p -> worst = (int *)malloc(sizeof(int));
table_p -> total = (int *)malloc(sizeof(int));
table_p -> average = (float *)malloc(sizeof(float));
table_p -> number_buckets = (int *)malloc(sizeof(int));

/*This is where I have isssue*/
for(i = 0; i < size; i++){
    table_p -> buckets_array[i] = NULL;
    table_p -> buckets_array[i] -> buckets_size = 0;

    if(table_p -> buckets_array[i] == '\0'){
      printf("ask this \n");
    }
}
4

2 回答 2

2

在您的代码中,您有:

table_p -> buckets_array[i] = NULL;
table_p -> buckets_array[i] -> buckets_size = 0;

这就像在说:

table_p -> buckets_array[i] = NULL;
NULL -> buckets_size = 0;

这不好。

于 2012-05-06T14:24:29.107 回答
0
if(table_p -> buckets_array[i] == '\0'){

我已经在您之前的(非常相似的)帖子中指出,它table_p->buckets_array[i]属于“指向 data_el 的指针”类型,并且 '\0' 是一个整数常量。请在发帖前阅读。

于 2012-05-06T15:21:56.160 回答