简体   繁体   English

void* (*)() 和 void* 有什么区别?

[英]What is the difference between void* (*)() and void*?

In one of my programs, I'm getting the error在我的一个程序中,我收到错误

invalid conversion from 'void* (*)()' to 'void*' [-fpermissive]

I'm trying to understand the difference between these two data types.我试图了解这两种数据类型之间的区别。 It is very confusing.这很令人困惑。

What I think that these data types are:我认为这些数据类型是:
void* (*)() So this data type is a pointer to a void ( void* ) which also has another pointer ( (*) ), and for some odd reason, it has empty parentheses. void* (*)()所以这个数据类型是一个指向 void ( void* ) 的指针,它还有另一个指针 ( (*) ),并且由于某些奇怪的原因,它有空括号。 So What I imagine the memory for this data type looks like is |--void address--|--pointer of unknown type--|--something denoting no arguments--|所以我想象这个数据类型的内存看起来是|--void address--|--pointer of unknown type--|--something denoting no arguments--|类型的|--void address--|--pointer of unknown type--|--something denoting no arguments--|

then there's void* .然后是void* This seems simple, and it only represents the address of a function.这看起来很简单,它只代表一个函数的地址。 The memory model should be as simple as |--void address--|内存模型应该像|--void address--|一样简单. .

Why this is so weird, is that the function that is thrown the error is as follows.为什么这么奇怪,是抛出错误的函数如下。

void callFunction(void *voidThing){
void *testVariable = voidThing;
}

And as far as I am aware, this creates a testVariable of type pointer, and that pointer is to a void.据我所知,这会创建一个指针类型的 testVariable,并且该指针指向 void。 This function also takes in a parameter of type pointer, and that pointer is also to a void.该函数还接受一个指针类型的参数,该指针也指向 void。

Why is the compiler throwing an error, even though the local variable is of the same type of the parameter?为什么编译器会抛出错误,即使局部变量与参数类型相同? What is the difference between the variables in terms of the memory footprint of the two?就两者的内存占用而言,变量之间有什么区别?

These are types :这些是类型

  • void * - pointer to void (this can point to any object, not to a function) void * - 指向 void 的指针(这可以指向任何对象,而不是函数)
  • void * () - function taking no arguments and returning void * void * () - 不带参数并返回void *函数
  • void * (*) () - pointer to function taking no arguments and returning void * void * (*) () - 指向不带参数并返回void *函数的指针

Example of a declaration of an identifier for each of the above, respectively:分别为上述各项声明标识符的示例:

void *object_ptr;    // variable: pointer to object

void *function();    // function  (not a variable)

void * (*function_pointer)();  // variable: pointer to function

The syntax for function types places the identifier before the argument list , a similar thing happens with arrays.函数类型的语法将标识符放在参数列表之前,数组也会发生类似的情况。 This is called infix notation .这称为中缀表示法

Pointer declarators are postfix , ie the identifier comes after the * .指针声明符是后缀,即标识符在*

In void * (*) () the first set of parentheses are necessary because void * *() would be a different type (the declaration grammar results in void * * staying together).void * (*) () ,第一组括号是必需的,因为void * *()将是不同的类型(声明语法导致void * *保持在一起)。


The callFunction function is correct in itself, however the error message probably comes from trying to call this with incorrect argument, eg the address of a function. callFunction函数本身是正确的,但是错误消息可能来自尝试使用不正确的参数调用它,例如函数的地址。 void * can only hold the address of an object . void *只能保存一个对象的地址。

Some platforms may allow reinterpret_cast to be used to cast an object pointer to a function pointer or vice versa, this feature is conditionally-supported with implementation-defined semantics , meaning that implementations may or may not allow it, but must document the behaviour if they do.某些平台可能允许使用reinterpret_cast将对象指针转换为函数指针,反之亦然,此功能由实现定义的语义有条件地支持,这意味着实现可能允许也可能不允许,但必须记录行为,如果它们做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM