参数转换:(正常)指向空指针,投需要? [英] Argument conversion: (normal) pointer to void pointer, cast needed?

查看:166
本文介绍了参数转换:(正常)指向空指针,投需要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当分配给或者从无效终场不需要投(C99§6.3.2.2子1 /§6.5.16.1子1)。指针,需要一个无效终场功能 - 传递(例如 INT )时,这是也是如此?

When assigning to or from a void-pointer no cast is needed (C99 §6.3.2.2 sub 1 / §6.5.16.1 sub 1). Is this also true when passing a (for example int-)pointer to a function that expects a void-pointer?

例如:

void foo(void * p){
    // Do something
}

int main(){
    int i = 12;
    foo(&i); // int * to void *: no cast needed?
}

当我编译这与海湾合作委员会(4.8.1,MinGW的-32)我得到既不错误,也不警告(与 -Wall &放大器; - 迂腐)。

When I compile this with GCC (4.8.1, MinGW-32) I get neither errors nor warnings (with -Wall & -pedantic).

在这个答案对比 该建议需要对这一呼吁铸造(消除 -Wformat 警告):

In contrast in this answer it is suggested that a cast is needed for this call (to eliminate -Wformat warnings):

int main(){
    int i = 12;
    printf("%p\n", &i);
}

但在我的情况下,海湾合作委员会不​​抱怨。

But in my case GCC doesn't complain.

所以:是通过非在需要的时候强制转换 - 无效终场以期望一个功能的无效 - 指针?

So: are casts needed when passing a non-void-pointer to a function that expects a void-pointer?

推荐答案

所不同的是的printf 是一个可变参数函数及参数可变型函数遵循其尾随参数不同的转换规则。

The difference is printf is a variadic function and variadic functions follow different conversion rules on their trailing arguments.

foo(&i); 

在这里为无需转换是一个原型的功能。 ç说&放大器;我转换为 P 的类型,如果分配和使用C之间存在一个隐含的所有的对象指针类型无效*

no cast is needed here as foo is a prototyped function. C says &i is converted to the type of p as if by assignment and in C there is an implicit between all object pointer types to void *.

的printf 的情况是像的printf 可变参数的功能对他们其余的参数和默认参数提升不同的无转换发生在指针类型的参数。

The case with printf is different as variadic functions like printf have default argument promotions on their remaining arguments and no conversion occur on the argument of pointer types.

C对原型的功能:

(C99,6.5.2.2p7)如果这表示调用的函数的前pression有一个类型,做包括一个原型,参数被隐式转换,仿佛被分配到种类对应的参数,每次取参数的类型是其声明的类型的不合格版本。

(C99, 6.5.2.2p7) "If the expression that denotes the called function has a type that does include a prototype, the arguments are implicitly converted, as if by assignment, to the types of the corresponding parameters, taking the type of each parameter to be the unqualified version of its declared type."

C对参数可变型函数:

C on variadic functions:

(C99,6.5.2.2p7)(C99,6.5.2.2p7)函数原型声明符引起的参数类型转换到最后声明参数后,停止省略号。默认参数促销活动上进行尾随
  论据。

(C99, 6.5.2.2p7) "(C99, 6.5.2.2p7) "The ellipsis notation in a function prototype declarator causes argument type conversion to stop after the last declared parameter. The default argument promotions are performed on trailing arguments."

所以:是通过一个非空指针,以期望一个空指针的函数时,需要强制转换

有关的printf P 转换说明需要一个无效* 参数。如果参数是不同的类型,函数调用调用未定义的行为。所以,如果 P 的参数是一个对象指针类型,(无效*)是必需的转换。

For printf, p conversion specifier requires a void * argument. If the argument is of a different type, the function call invokes undefined behavior. So if the argument of p is an object pointer type, the (void *) cast is required.

(C99,7.19.6.1p8)P参数应是无效的指针。

(C99, 7.19.6.1p8) "p The argument shall be a pointer to void."

这篇关于参数转换:(正常)指向空指针,投需要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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