我需要了解爲什麼分配void*
到int**
將起作用,但是當打印int**
的值時,它將不起作用。下面的代碼:分配空白到雙指針
int k = 3;
int *l = &k;
void *a = l;
int **c = a; // if I assign, &a, the below line work, but get warning: incompatible
// pointer type.
printf("%d", **c); // Code break;
,但此行
printf("%d", *c); // will work, but get warning "%d expect int but of type int *".
更新:我看到這種類型的代碼在OOC book
這裏筆者做:
struct Class {
size_t size;
int (*differ)(const void *self, const void *b);
..... Some more code ....
};
int differ(const void *self, const void *b) {
// Note this line
const struct Class **cp = self;
assert(self && *cp && (*cp)->differ);
return (*cp)->differ(self, b);
}
** c將嘗試讀取地址'3'中的整數 – Bgie