[英]idiomatic C for const double-pointers
我知道在 C 中,您不能将例如char**
隐式转换为const char**
(参见C-Faq , SO question 1 , SO Question 2 )。
另一方面,如果我看到一个 function 声明如下:
void foo(char** ppData);
我必须假设 function 可能会更改传入的数据。因此,如果我正在编写不会更改数据的 function,我认为最好声明:
void foo(const char** ppData);
甚至:
void foo(const char * const * ppData);
但这会让 function 的用户陷入尴尬的 position 中。 他们可能有:
int main(int argc, char** argv)
{
foo(argv); // Oh no, compiler error (or warning)
...
}
为了干净地调用我的 function,他们需要插入一个演员表。
我主要来自 C++ 背景,由于 C++ 更深入的 const 规则,这不是一个问题。
C 中的惯用解决方案是什么?
将 foo 声明为采用char**
,并仅记录它不会更改其输入的事实? 这似乎有点恶心,尤其是。 因为它会惩罚可能拥有想要传递它的const char**
的用户(现在他们必须抛弃const-ness)
强制用户转换他们的输入,添加 const-ness。
还有什么?
虽然你已经接受了答案,但我想找到3)即宏。 你可以用你的函数用户只写一个调用foo(x);
的方式来编写它们foo(x);
其中x可以是const
-qualified或不是。 这个想法将有一个宏CASTIT
执行转换并检查参数是否是有效类型,另一个是用户界面:
void totoFunc(char const*const* x);
#define CASTIT(T, X) ( \
(void)sizeof((T const*){ (X)[0] }), \
(T const*const*)(X) \
)
#define toto(X) totoFunc(CASTIT(char, X))
int main(void) {
char * * a0 = 0;
char const* * b0 = 0;
char *const* c0 = 0;
char const*const* d0 = 0;
int * * a1 = 0;
int const* * b1 = 0;
int *const* c1 = 0;
int const*const* d1 = 0;
toto(a0);
toto(b0);
toto(c0);
toto(d0);
toto(a1); // warning: initialization from incompatible pointer type
toto(b1); // warning: initialization from incompatible pointer type
toto(c1); // warning: initialization from incompatible pointer type
toto(d1); // warning: initialization from incompatible pointer type
}
CASTIT
宏看起来有点复杂,但它只是首先检查X[0]
是否与char const*
兼容。 它使用复合文字。 然后将其隐藏在sizeof
以确保实际上永远不会创建复合文字,并且X
不会被该测试评估。
然后是一个普通的演员,但这本身就太危险了。
正如您在main
示例中所看到的,这可以准确地检测出错误的情况。
宏可以实现很多这样的东西。 我最近用const
限定数组编写了一个复杂的例子。
2比1好.1虽然很常见,因为大量的C代码根本不使用const。 因此,如果您正在为新系统编写新代码,请使用2.如果您正在为const很少的现有系统编写维护代码,请使用1。
使用选项2.选项1具有您提到的缺点,并且类型安全性较低。
如果我看到一个带有char **
参数的函数,并且我有一个char *const *
或类似的char *const *
,我会复制并传递它,以防万一。
现代(C11+)方式使用_Generic
来保持类型安全和 function 指针:
// joins an array of words into a new string;
// mutates neither *words nor **words
char *join_words (const char *const words[])
{
// ...
}
#define join_words(words) join_words(_Generic((words),\
char ** : (const char *const *)(words),\
char *const * : (const char *const *)(words),\
default : (words)\
))
// usage :
int main (void)
{
const char *const words_1[] = {"foo", "bar", NULL};
char *const words_2[] = {"foo", "bar", NULL};
const char *words_3[] = {"foo", "bar", NULL};
char *words_4[] = {"foo", "bar", NULL};
// none of the calls generate warnings:
join_words(words_1);
join_words(words_2);
join_words(words_3);
join_words(words_4);
// type-checking is preserved:
const int *const numbers[] = { (int[]){1, 2}, (int[]){3, 4}, NULL };
join_words(numbers);
// warning: incompatible pointer types passing
// 'const int *const [2]' to parameter of type 'const char *const *'
// since the macro is defined after the function's declaration and has the same name,
// we can also get a pointer to the function
char *(*funcptr) (const char *const *) = join_words;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.