为什么我的整个数组都有'\ 0'?

时间:2012-05-06 14:18:27

标签: c arrays pointers data-structures structure

这给我的程序带来了很多问题。当我创建一个新的结构化指针数组时,它们都等于'\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");
    }
}

2 个答案:

答案 0 :(得分: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;

这不好。

答案 1 :(得分:0)

if(table_p -> buckets_array[i] == '\0'){

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

相关问题