【发布时间】:2013-07-18 00:50:00
【问题描述】:
pthread_create(&Thread,NULL,ChildThread,(void *)100);
1) 我们可以像上图那样传递 pthread_create 的第四个参数吗?不应该是指针变量吗?
【问题讨论】:
-
但是你把它屏蔽成一个 void *,它会运行正常。
-
它是如何工作的?我们不能将数字转换为指针吗?
-
您可以将数字转换为指针,但是当您尝试将其恢复为数字时,您的状态会很糟糕,具体取决于架构,您可能会遇到问题。
-
C 允许您在指针和整数之间进行转换,只要指针值可以由整数类型保存。但是,结果是实现定义的。如果指针值大于整数类型可以容纳的值,则结果未定义。
-
@user1762571 - 您可以将任何整数转换为 void*,但反之则很危险,将 void* 转换为 int 是不可移植的。在 64 位架构上,指针为 64 位宽,但 int 仅为 32 位。
Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.