gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 16:24:45 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com