void *是否总是与char *具有相同的表示形式? [英] Will a void* always have the same representation as a char*?

查看:84
本文介绍了void *是否总是与char *具有相同的表示形式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

void*始终与char*具有相同的表示形式吗?

Will a void* always have the same representation as a char* ?

详细信息:

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

I want to work with a variadic function that takes char*'s terminated by a (char*)0 like so:

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 :

I wanted to replace (char*)0 with NULL, but judging from http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf's:

66)宏NULL在(和其他标头中)定义为 空指针常量;参见7.19.

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

3整数常量表达式,其值为0,或者这样的整数 转换为void *类型的表达式称为空指针常量. 66) 如果将空指针常量转换为指针类型,则 结果指针(称为空指针)可以保证进行比较 不等于指向任何对象或函数的指针.

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是不可接受的.

I can't, because in the variadic context, I absolutely need a char* and a plain 0 is unacceptable.

如果我定义了:

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

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

would it be legal for me to use it to terminate my variadic(char*,...)?

推荐答案

C11,§6.2.5,¶28(草稿

C11, §6.2.5, ¶28 (draft N1570) says:

指向void的指针应具有相同的表示形式和对齐方式 要求作为指向字符类型的指针. 48)同样,指针 兼容类型的合格或不合格版本应具有 相同的表示和对齐要求.

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.

(重点是我的).

这篇关于void *是否总是与char *具有相同的表示形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆