Typedef函数指针?

守給你的承諾、 提交于 2020-02-26 22:57:02

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

typedef void (*FunctionFunc)();

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

  1. 为什么要使用typedef
  2. 语法看起来很奇怪; 在void之后,应该没有函数名称或其他名称吗? 它看起来像一个匿名函数。
  3. 是否创建了函数指针来存储函数的内存地址?

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


#1楼

#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) );
}

#2楼

  1. typedef用于别名类型; 在这种情况下,您要将FunctionFunc别名为void(*)()

  2. 确实,语法看起来确实很奇怪,请看以下内容:

    typedef void (*FunctionFunc) ( ); // ^ ^ ^ // return type type name arguments
  3. 不,这只是告诉编译器FunctionFunc类型将是一个函数指针,它没有定义一个函数指针,如下所示:

    FunctionFunc x; void doSomething() { printf("Hello there\\n"); } x = &doSomething; x(); //prints "Hello there"

#3楼

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

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


#4楼

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)();

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

困难在于在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

#5楼

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

using FunctionFunc = std::function<void(int arg1, std::string arg2)>;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!