void (*)()是什么?

incident_a 2014-04-25 09:38:36
(void (*)())代表类型转换符,那
void (*)()怎么表示它?什么都不是?
...全文
1911 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-04-25
  • 打赏
  • 举报
回复
引用 10 楼 incident_a 的回复:
大家先看看我想引出的问题: 为什么 #define funcptr void (*)()不能表示为 typedef void (*)() funcptr;????? 因为我认为如果void(*)()是一种类型的话,上面的语句不应该出错才对
那是因为你算符优先级不熟:
//C++ Operators
//  Operators specify an evaluation to be performed on one of the following:
//    One operand (unary operator)
//    Two operands (binary operator)
//    Three operands (ternary operator)
//  The C++ language includes all C operators and adds several new operators.
//  Table 1.1 lists the operators available in Microsoft C++.
//  Operators follow a strict precedence which defines the evaluation order of
//expressions containing these operators.  Operators associate with either the
//expression on their left or the expression on their right;    this is called
//“associativity.” Operators in the same group have equal precedence and are
//evaluated left to right in an expression unless explicitly forced by a pair of
//parentheses, ( ).
//  Table 1.1 shows the precedence and associativity of C++ operators
//  (from highest to lowest precedence).
//
//Table 1.1   C++ Operator Precedence and Associativity
// The highest precedence level is at the top of the table.
//+------------------+-----------------------------------------+---------------+
//| Operator         | Name or Meaning                         | Associativity |
//+------------------+-----------------------------------------+---------------+
//| ::               | Scope resolution                        | None          |
//| ::               | Global                                  | None          |
//| [ ]              | Array subscript                         | Left to right |
//| ( )              | Function call                           | Left to right |
//| ( )              | Conversion                              | None          |
//| .                | Member selection (object)               | Left to right |
//| ->               | Member selection (pointer)              | Left to right |
//| ++               | Postfix increment                       | None          |
//| --               | Postfix decrement                       | None          |
//| new              | Allocate object                         | None          |
//| delete           | Deallocate object                       | None          |
//| delete[ ]        | Deallocate object                       | None          |
//| ++               | Prefix increment                        | None          |
//| --               | Prefix decrement                        | None          |
//| *                | Dereference                             | None          |
//| &                | Address-of                              | None          |
//| +                | Unary plus                              | None          |
//| -                | Arithmetic negation (unary)             | None          |
//| !                | Logical NOT                             | None          |
//| ~                | Bitwise complement                      | None          |
//| sizeof           | Size of object                          | None          |
//| sizeof ( )       | Size of type                            | None          |
//| typeid( )        | type name                               | None          |
//| (type)           | Type cast (conversion)                  | Right to left |
//| const_cast       | Type cast (conversion)                  | None          |
//| dynamic_cast     | Type cast (conversion)                  | None          |
//| reinterpret_cast | Type cast (conversion)                  | None          |
//| static_cast      | Type cast (conversion)                  | None          |
//| .*               | Apply pointer to class member (objects) | Left to right |
//| ->*              | Dereference pointer to class member     | Left to right |
//| *                | Multiplication                          | Left to right |
//| /                | Division                                | Left to right |
//| %                | Remainder (modulus)                     | Left to right |
//| +                | Addition                                | Left to right |
//| -                | Subtraction                             | Left to right |
//| <<               | Left shift                              | Left to right |
//| >>               | Right shift                             | Left to right |
//| <                | Less than                               | Left to right |
//| >                | Greater than                            | Left to right |
//| <=               | Less than or equal to                   | Left to right |
//| >=               | Greater than or equal to                | Left to right |
//| ==               | Equality                                | Left to right |
//| !=               | Inequality                              | Left to right |
//| &                | Bitwise AND                             | Left to right |
//| ^                | Bitwise exclusive OR                    | Left to right |
//| |                | Bitwise OR                              | Left to right |
//| &&               | Logical AND                             | Left to right |
//| ||               | Logical OR                              | Left to right |
//| e1?e2:e3         | Conditional                             | Right to left |
//| =                | Assignment                              | Right to left |
//| *=               | Multiplication assignment               | Right to left |
//| /=               | Division assignment                     | Right to left |
//| %=               | Modulus assignment                      | Right to left |
//| +=               | Addition assignment                     | Right to left |
//| -=               | Subtraction assignment                  | Right to left |
//| <<=              | Left-shift assignment                   | Right to left |
//| >>=              | Right-shift assignment                  | Right to left |
//| &=               | Bitwise AND assignment                  | Right to left |
//| |=               | Bitwise inclusive OR assignment         | Right to left |
//| ^=               | Bitwise exclusive OR assignment         | Right to left |
//| ,                | Comma                                   | Left to right |
//+------------------+-----------------------------------------+---------------+
#define funcptr void (*)()可以表示为 typedef void (funcptr*)();
均陵鼠侠 2014-04-25
  • 打赏
  • 举报
回复
引用 18 楼 incident_a 的回复:
[quote=引用 16 楼 sholber 的回复:] [quote=引用 11 楼 incident_a 的回复:] 那你说,(void(*)()) 0 这里的(void(*)())怎么表述?
那不是“指向返回类型为void的函数”的指针么?[/quote] 照你这么说,(void(*)())就是某个指针,就暂且叫它为ptr吧,那么 ptr0是什么意思? 这个问题的来源是《C陷阱与缺陷》第二章的问题,完整语句如下: (*void(*)())0();[/quote] 什么乱七八糟的。我前面说得很清楚,void(*)()是类型名。类型名的用处之一是强制类型转换: (type-name )cast-expression 注意那个括号。ptr0和(ptr)0完全不是一回事嘛。
均陵鼠侠 2014-04-25
  • 打赏
  • 举报
回复
引用 17 楼 incident_a 的回复:
typedef int Type; 我就是根据这个形式推断出: typedef void(*)() Type; 上面的int和void(*)()不应该是相似的吗?
你老师教你的,和你从教材上看的,都是这样的声明:
int i;
struct s s1;
他们没讲复杂一些的声明:
int (*p) [2];
void (*p)(int,int);
所以你的问题不在于不懂typedef,而在于不懂声明。
incident_a 2014-04-25
  • 打赏
  • 举报
回复
抱歉上面打错了,应该是 (*(void(*)())0)();
incident_a 2014-04-25
  • 打赏
  • 举报
回复
引用 16 楼 sholber 的回复:
[quote=引用 11 楼 incident_a 的回复:] 那你说,(void(*)()) 0 这里的(void(*)())怎么表述?
那不是“指向返回类型为void的函数”的指针么?[/quote] 照你这么说,(void(*)())就是某个指针,就暂且叫它为ptr吧,那么 ptr0是什么意思? 这个问题的来源是《C陷阱与缺陷》第二章的问题,完整语句如下: (*void(*)())0();
incident_a 2014-04-25
  • 打赏
  • 举报
回复
typedef int Type; 我就是根据这个形式推断出: typedef void(*)() Type; 上面的int和void(*)()不应该是相似的吗?
均陵鼠侠 2014-04-25
  • 打赏
  • 举报
回复
引用 11 楼 incident_a 的回复:
那你说,(void(*)()) 0 这里的(void(*)())怎么表述?
那不是“指向返回类型为void的函数”的指针么?
均陵鼠侠 2014-04-25
  • 打赏
  • 举报
回复
引用 10 楼 incident_a 的回复:
大家先看看我想引出的问题: 为什么 #define funcptr void (*)()不能表示为 typedef void (*)() funcptr;????? 因为我认为如果void(*)()是一种类型的话,上面的语句不应该出错才对
什么你想引出什么,难道没有人告诉你,类型定义的本质是“将标识符定义为[color=#FF0000]它被声明的那种类型[/color]”吗?所以,类型定义的形式类似于声明。知道了这一点,你再研究一下如何声明一个函数类型,就明白了。
incident_a 2014-04-25
  • 打赏
  • 举报
回复
大家先等等,看看楼主的最新疑问 为什么 #define funcptr void (*)()不能表示为 typedef void (*)() funcptr; 还有 (void(*)()) 0 这里的(void(*)())怎么表述?
mujiok2003 2014-04-25
  • 打赏
  • 举报
回复
void (*)()函数地址,类型为void ()
均陵鼠侠 2014-04-25
  • 打赏
  • 举报
回复
引用 4 楼 PDD123 的回复:
void(*)()是一个返回值为void,参数为空的函数指针类型 void(*abc)();就是定义了一个“返回值为void,参数为空”的函数指针abc,可以这样调用:abc();
	void * def;
	void(*abc)();
	abc=(void(*)())def;
这一段能通过编译,最后一句,就是把def的类型强制转化成void(*)(),并赋给abc
你凭什么说它的参数为空?拿出你的权威出处来。对了,你如何用参数为空解释这个:
int main (void)
{
    void f();
    f(2);
}

void f (int i)
{
    printf("%d\n", i);
}
incident_a 2014-04-25
  • 打赏
  • 举报
回复
那你说,(void(*)()) 0 这里的(void(*)())怎么表述?
incident_a 2014-04-25
  • 打赏
  • 举报
回复
大家先看看我想引出的问题: 为什么 #define funcptr void (*)()不能表示为 typedef void (*)() funcptr;????? 因为我认为如果void(*)()是一种类型的话,上面的语句不应该出错才对
均陵鼠侠 2014-04-25
  • 打赏
  • 举报
回复
引用 2 楼 incident_a 的回复:
[quote=引用 1 楼 tuzhutuzhu 的回复:] (void (*)( )) ,是一个返回值为void,参数为空的函数指针原型。
表示“指向返回值为浮点类型的函数的指针”的类型转换符, 而且你还没说void(*)()是什么呢[/quote] 头回听说还有什么“类型转换符”,你发明的?
均陵鼠侠 2014-04-25
  • 打赏
  • 举报
回复
引用 1 楼 tuzhutuzhu 的回复:
(void (*)( )) ,是一个返回值为void,参数为空的函数指针原型。
头回听说指针也有原型了。
均陵鼠侠 2014-04-25
  • 打赏
  • 举报
回复
引用 1 楼 tuzhutuzhu 的回复:
(void (*)( )) ,是一个返回值为void,参数为空的函数指针原型。
说话之前 一定要先想好,否则容易传播错误的知识。 首先,(void (*)( )) 是一个类型名(type name),而且指定的是一个指针类型,又不是函数类型。 其次,那一对空的括号并不意味着参数为空。N1570,6.7.6.3 Function declarators (including prototypes): The empty list in a function declarator that is not part of a definition of that function specifies that no information about the number or types of the parameters is supplied. 这就是说,如果在函数声明中使用空的参数列表,则意味着未提供参数的数量或类型信息,但并不是无参数。从C99开始,如果函数没有参数,建议使用(void)。
int main (void)
{
    void f();
    f(2);
}

void f (int i)
{
    printf("%d\n", i);
}
incident_a 2014-04-25
  • 打赏
  • 举报
回复
引用 5 楼 a515360208 的回复:
函数名就是一个指针,void fun(); fun的类型就是void(*)();
按照你的说法,为什么 #define funcptr void (*)()不能表示为 typedef void (*)() funcptr; ?????
漂浮一生 2014-04-25
  • 打赏
  • 举报
回复
函数名就是一个指针,void fun(); fun的类型就是void(*)();
PDD123 2014-04-25
  • 打赏
  • 举报
回复
void(*)()是一个返回值为void,参数为空的函数指针类型 void(*abc)();就是定义了一个“返回值为void,参数为空”的函数指针abc,可以这样调用:abc();
	void * def;
	void(*abc)();
	abc=(void(*)())def;
这一段能通过编译,最后一句,就是把def的类型强制转化成void(*)(),并赋给abc
incident_a 2014-04-25
  • 打赏
  • 举报
回复
讨论的不是函数原型
加载更多回复(8)

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧