Typedef函數指針?

我正在學習如何動態加載DLL,但我不明白的是這一行 數組

typedef void (*FunctionFunc)();

我有幾個問題。 若是有人可以回答他們,我將不勝感激。 app

  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. 確實,語法看起來確實很奇怪,請看如下內容: ui

    typedef void (*FunctionFunc) ( ); // ^ ^ ^ // return type type name arguments
  3. 不,這只是告訴編譯器FunctionFunc類型將是一個函數指針,它沒有定義一個函數指針,以下所示: spa

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

#3樓

若是沒有typedef字,則在C ++中,聲明將聲明類型爲無參數FunctionFunc的指針類型的FunctionFunc變量,並返回voidprototype

使用typedef它將FunctionFunc定義爲該類型的名稱。 指針


#4樓

typedef是一種將名稱與類型相關聯的語言構造。
例如,您可使用與原始類型相同的方式來使用它 code

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

使用它們像 orm

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)>;
相關文章
相關標籤/搜索