c - void* 是否始终具有与 char* 相同的表示形式?

标签 c pointers language-lawyer

void* 是否总是与 char* 具有相同的表示形式?

详细信息:

我想使用一个可变参数函数,该函数采用 (char*)0 终止的 char*,如下所示:

int variadic(char*, ...); //<-prototype
variadic("foo", "bar", (char*)0); //<- usage

我想用NULL替换(char*)0,但是从 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf的:

66) The macro NULL is defined in (and other headers) as a null pointer constant; see 7.19.

3 An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. 66) If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.

我不能,因为在 variadic 上下文中,我绝对需要一个 char* 而一个普通的 0 是 Not Acceptable 。

如果我定义:

#define NIL (void*)0 /*<= never a plain 0*/

我用它来终止我的 variadic(char*,...) 是否合法?

最佳答案

C11,§6.2.5,¶28(草案 N1570)说:

A pointer to void shall have the same representation and alignment requirements as a pointer to a character type. 48) Similarly, pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements.

(强调我的)。

关于c - void* 是否始终具有与 char* 相同的表示形式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39872474/

相关文章:

c - 从文件中读取并进行计算。 C程序

c - 帮助破译简单的汇编代码

c - 删除 C 结构中指针的常量性

c++ - std::map 中的对象指针在通过 getter 检索时损坏

C如何将用户输入的单词存储在字符指针中

c - 处理游戏服务器的多个客户端

c++ - 指向限定和非限定类型表示的指针

c++ - C++中静态变量的个数有限制吗

c - 指向指针衰减的数组是否更改为指针对象?

c - Scanf 在仅加载数字的程序中不会注意到 '\n' 字符