我有以下代码:
/*
* Pointer to a function that reads a codesegment
*/
typedef bool (*BRCS)(void *, uint32, uint64 *, uint64 *, const char **, const char **);
BRCS get_prog_id;
/*
* 'get_prog_id' is loaded from a dynamic library
*/
uint64 start_o;
uint64 length_o;
char prog_id[256];
char err[256];
get_prog_id(NULL, 0, &start_o, &length_o, &prog_id, &err);
当我运行我的编译器时,我收到以下警告:
passing argument 5 of get_prog_id from incompatible pointer type
passing argument 6 of get_prog_id from incompatible pointer type
因此,它提示我的最后两个参数没有 char **
。
我很困惑。我的理解是,表示 TYPES
数组的变量等同于指向 TYPE
的指针。因此,应用 &
运算符会给您一个指向 TYPE
的指针。
我在这里错过了什么?
最佳答案
这里有两个问题:
(1)
&prog_id
的类型不是char *
,而是char (*)[256]
;即指向长度为 256 的字符数组的指针。
(2)
即使您可以获得 char **
(例如 char *prog_id = malloc(256); &prog_id
),char **
不与 const char **
兼容,原因有些模糊。最好的解释可能在这里:http://c-faq.com/ansi/constmismatch.html .
关于c - 对数组变量的引用是什么类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4948474/