【发布时间】:2012-05-06 14:18:27
【问题描述】:
这给我的程序带来了很多问题。为什么当我创建一个新的结构化指针数组时它们都等于'\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");
}
}
【问题讨论】:
-
如果我说
NULL与\0完全相同会有帮助吗?所以如果你这样做buckets_array[i] = NULL;,那么是的,然后是buckets_array[i] == '\0'。 -
是的,这真的会回答我的问题。谢谢
-
好的。嗯,它们并不完全相同,它们有不同的类型。但同样的价值!
-
“final member is '\0'”约定仅适用于字符串,因为 C 中的“字符串”只是一个以 '\0' 结尾的字符数组。打破它,你得到的是单引号意味着你正在指定一个字符,但反斜杠意味着将它作为一个字符转义并使用 0 的值;所以你可以说(我已经看到,令我害怕)“mychar = 0”并获得相同的效果,但会为读者失去重要的上下文线索。
-
@tbert 是的,C 对此实在是太宽容了。你甚至可以写
mychar = 45.6;而不会出现错误或警告。不适合初学者。
标签: c arrays pointers data-structures structure