2012-04-07 66 views
0
#include <stdio.h> 
#include <string.h> 

void func1 (void) { printf("1\n"); } 
void func0 (void) { printf("0\n"); } 

typedef struct { 
    void (*func0)(void); 
    void (*func1)(void); 
}mainJT; 

static const mainJT coreJT = { 
    core_func0, 
    core_func1 
}; 

mainJT currJT; 

int main() 
{ 
    currJT=coreJT; 
    coreJT.core_func0(); 
    getchar(); 
    return 0; 
} 

請幫我修復錯誤,我相信我會犯一些明顯的錯誤。謝謝。函數指針在C語言中的使用錯誤

回答

0

你的問題不是很清楚,但我看看我能找到。

typedef struct { 
    void (*func0)(void); 
    void (*func1)(void); 
} mainJT; 

這裏你聲明與函數指針成員func0func1一個結構。然後,您試圖通過初始化列表來定義coreJT變量:

static const mainJT coreJT = { 
    core_func0, 
    core_func1 
}; 

但是,這並不工作,因爲沒有所謂的core_func0core_func1功能! 您也可以嘗試撥打

coreJT.core_func0();

這也是不正確的,因爲你的結構沒有名稱的成員core_func0


對於一個可能的解決方案嘗試重命名你的功能,像這樣:

void core_func1 (void) { printf("1\n"); } 
void core_func0 (void) { printf("0\n"); } 

coreJT.func0調用你的函數指針();

+0

請讓我知道我應該如何重新命名的功能。它可以幫助您發佈整個代碼。謝謝。 – user1128265 2012-04-07 18:07:17

+0

@ user1128265我只是看着你的另一個問題,這可能是基本問題,所以請閱讀我的[答案](http://stackoverflow.com/questions/10014284/what-does-null-function-pointer-in-c -mean/10056987#10056987)第一個 – Anthales 2012-04-07 18:11:12

+0

謝謝Anthales,幫助! – user1128265 2012-04-08 02:43:12

0

我看到很多的錯誤: 爲例如:初始化結構的正確的方法是: /*定義一種類型的點爲與整數成員的x,y *一個struct/

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

/* Define a variable p of type point, and initialize all its members inline! */ 
point p = {1,2}; 

所以您的部分代碼是:

mainJT coreJT = { 
    core_func0; 
    core_func1; 
}; 

完全錯誤。

另外,函數core_func1 core_func0是聲明和定義的,我無法看到它們。

我想你首先需要經過structures in c