ChatGPT解决这个技术问题 Extra ChatGPT

typedef函数指针?

我正在学习如何动态加载 DLL,但我不明白的是这一行

typedef void (*FunctionFunc)();

我有几个问题。如果有人能够回答他们,我将不胜感激。

为什么使用 typedef?语法看起来很奇怪; void 之后不应该有函数名什么的吗?它看起来像一个匿名函数。是否创建函数指针来存储函数的内存地址?

所以我现在很困惑;你能为我澄清一下吗?

查看链接(最后一部分)learncpp.com/cpp-tutorial/78-function-pointers
需要注意的是,由于 c++11 可以使用 using FunctionFunc = void (*)(); 代替。更清楚一点,您只是在声明类型的名称(指向函数的指针)
只是为了添加到@user362515,对我来说更清晰的形式是:using FunctionFunc = void(void);
@topspin IIRC 这两个不一样。一种是函数指针类型,另一种是函数类型。存在隐式转换,这就是它起作用的原因,IANA(C++)L 所以,可以介入并纠正我。无论如何,如果打算定义一个指针类型,我认为 * 的语法更明确一点。
下面是 a related question I asked a long time ago,说明为什么 myFuncPtr()(*myFuncPtr)() 都是有效的函数调用。

Y
Yun

typedef 是一种将名称与类型相关联的语言结构。
您可以像使用原始类型一样使用它,例如

typedef int myinteger;
typedef char *mystring;
typedef void (*myfunc)();

像使用它们一样

myinteger i;   // is equivalent to    int i;
mystring s;    // is the same as      char *s;
myfunc f;      // compile equally as  void (*f)();

如您所见,您可以将 typedefed 名称替换为上面给出的定义。

难点在于C和C++中指向函数语法和可读性的指针,而typedef可以提高此类声明的可读性。然而,语法是合适的,因为函数 - 不像其他更简单的类型 - 可能有一个返回值和参数,因此有时需要冗长而复杂的函数指针声明。

对于指向函数数组的指针和其他一些更间接的风格,可读性可能开始变得非常棘手。

回答你的三个问题

为什么使用 typedef?为了便于阅读代码——特别是对于指向函数的指针或结构名称。

语法看起来很奇怪(在指向函数声明的指针中)这种语法并不明显,至少在开始时是这样。使用 typedef 声明来简化阅读

是否创建函数指针来存储函数的内存地址?是的,函数指针存储函数的地址。这与 typedef 结构无关,它只会简化程序的写入/读取;编译器只是在编译实际代码之前扩展 typedef 定义。

例子:

typedef int (*t_somefunc)(int,int);

int product(int u, int v) {
  return u*v;
}

t_somefunc afunc = &product;
...
int x2 = (*afunc)(123, 456); // call product() to calculate 123*456

在最后一个例子中,'square' 不仅仅指同一个东西,即指向函数的指针,而不是使用 &square。
问题,在您的第一个 typedef 示例中,您的格式为 typedef type alias,但使用函数指针似乎只有 2 个参数,typedef type。别名是否默认为类型参数中指定的名称?
@pranavk:是的,square&square(实际上,*square**square)都引用同一个函数指针。
@user814628:不清楚你在问什么。使用 typedef int newname,您将 newname 变成 int 的别名。使用 typedef int (*func)(int),您将 func 变成 int (*)(int) 的别名 — 一个指向函数的指针,该函数接受一个 int 参数并返回一个 int 值。
我想我只是对订购感到困惑。使用 typedef int (*func)(int),我知道 func 是一个别名,只是有点困惑,因为别名与类型纠缠不清。以 typedef int INT 为例,如果 typedef 函数指针的形式为 typedef int(*function)(int) FUNC_1,我会更容易。这样我就可以在两个单独的令牌中看到类型和别名,而不是被网格化为一个。
l
lineil

typedef 用于给类型起别名;在这种情况下,您将 FunctionFunc 别名为 void(*)()。确实语法看起来很奇怪,看看这个: typedef void (*FunctionFunc) ( ); // ^ ^ ^ // 返回类型名称参数 不,这只是告诉编译器 FunctionFunc 类型将是一个函数指针,它没有定义一个,像这样:FunctionFunc x; void doSomething() { printf("你好\n"); } x = &doSomething; X(); //打印“你好”


typedef声明新类型。您可以有许多由 typedef 定义的相同类型的名称,并且它们 不同(例如,函数重载)。在某些情况下,就如何使用名称而言,typedef 定义的名称并不完全等同于其定义的名称,但同一名称的多个 typedef 定义名称是等价的。
B
BartoszKP

如果没有 typedef 字,在 C++ 中,声明将声明一个变量 FunctionFunc,其类型为指向无参数函数的指针,返回 void

使用 typedef 时,它将 FunctionFunc 定义为该类型的名称。


H
Halil Kaskavalci

如果您可以使用 C++11,您可能需要使用 std::functionusing 关键字。

using FunctionFunc = std::function<void(int arg1, std::string arg2)>;

没有 C++11 using 关键字就像 typedef std::function<void(int, std::string)> FunctionFunc;,以防万一有人想要另一个没有 C++11 的函数的包装器
A
Amjad
#include <stdio.h>
#include <math.h>

/*
To define a new type name with typedef, follow these steps:
1. Write the statement as if a variable of the desired type were being declared.
2. Where the name of the declared variable would normally appear, substitute the new type name.
3. In front of everything, place the keyword typedef.
*/

// typedef a primitive data type
typedef double distance;

// typedef struct 
typedef struct{
    int x;
    int y;
} point;

//typedef an array 
typedef point points[100]; 

points ps = {0}; // ps is an array of 100 point 

// typedef a function
typedef distance (*distanceFun_p)(point,point) ; // TYPE_DEF distanceFun_p TO BE int (*distanceFun_p)(point,point)

// prototype a function     
distance findDistance(point, point);

int main(int argc, char const *argv[])
{
    // delcare a function pointer 
    distanceFun_p func_p;

    // initialize the function pointer with a function address
    func_p = findDistance;

    // initialize two point variables 
    point p1 = {0,0} , p2 = {1,1};

    // call the function through the pointer
    distance d = func_p(p1,p2);

    printf("the distance is %f\n", d );

    return 0;
}

distance findDistance(point p1, point p2)
{
distance xdiff =  p1.x - p2.x;
distance ydiff =  p1.y - p2.y;

return sqrt( (xdiff * xdiff) + (ydiff * ydiff) );
}

在什么情况下需要'&'?例如,'func_p = &findDistance' 和 'func_p = findDistance' 是否同样有效?
函数名是一个指针,所以你不需要使用'&'。换句话说,当考虑函数指针时,'&' 是可选的。但是,对于原始数据类型,您需要使用“&”来获取其地址。
'&' 总是让人感到奇怪可以是可选的。如果使用 typedef 创建指向原始类型的指针(即 typedef (*double) p,'&' 是可选的吗?
我想你想输入 typedef double* p 来定义一个指向双精度的指针。如果要从原始变量填充 p 指针,则需要使用“&”。附注,我们你 *<指针名称>取消引用指针。 * 在函数指针中用于取消引用指针(函数名)并转到函数指令所在的内存块。
a
alinsoar

对于语法的一般情况,您可以查看 annex A of the ANSI C standard

从那里的 Backus-Naur 表单中,您可以看到 typedef 的类型为 storage-class-specifier

在类型 declaration-specifiers 中,您可以看到可以混合多种说明符类型,它们的顺序无关紧要。

例如,正确的说法是,

long typedef long a;

将类型 a 定义为 long long 的别名。因此,要了解详尽使用的 typedef,您需要查阅一些定义语法的 backus-naur 形式(ANSI C 有许多正确的语法,而不仅仅是 ISO 的语法)。

当您使用 typedef 为函数类型定义别名时,您需要将别名放在放置函数标识符的相同位置。在您的情况下,您将类型 FunctionFunc 定义为指向函数的指针的别名,该函数的类型检查在调用时被禁用并且不返回任何内容。