2016-11-29 60 views
0

進出口工作試圖用函數指針的力量,這一切都很好,直到我試圖使函數指針使用第二個參數爲int類型。函數指針不是INT

下面的代碼生成一個錯誤,其上顯示下面 在頭文件:

#include <stddef.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

typedef struct UnitTag { 
    int x; 
    int y; 
    void (*move)(Unit, int); 
} Unit; 

錯誤:

error: expected ‘)’ before ‘int’ 
    void (*move)(Unit, int); 
         ^

空隙(*移動)(單位);工作都很好,這讓我驚訝地發現,如何添加參數會導致錯誤。

我打電話給我的結構在C文件,通過包括頭,然後做: 單位單元[UNITCOUNT]; 單元[0] .move(&單元[0],1);

更新:

補充說:

typedef struct UnitTag Unit 

導致錯誤dissapear,不過,我可以像以前一樣不再使用該功能。

error: incompatible type for argument 1 of ‘units[i].move’ 
    units[0].move(&units[0], 0); 
^

注:應爲「單位」,但參數的類型的「結構UnitTag *」

+0

它應該是void(* move)(struct UnitTag,int);我不知道如何失效(*移動)(單位);正在爲你工作。 – MayurK

+0

@MayurK,它甚至給出了相同的錯誤使用時 「空隙(*移動)(UnitTag,INT);」 – LamaCoder

+0

@LamaCoder你錯過了'struct'關鍵字。 'void(* move)(struct UnitTag,int);' – aschepler

回答

3

如果我得到你,你可以簡單地使用struct關鍵字:

#include <stdio.h> 

typedef struct UnitTag { 
    int x; 
    int y; 
    void (*move)(struct UnitTag, int); 
} Unit; 

void Test (struct UnitTag test1, int test2) 
{ 
    printf("Test1.x: %d\n", test1.x); 
    printf("Test1.y: %d\n", test1.y); 
    printf("Test2 : %d\n", test2); 
} 

int main(void) 
{ 
    Unit units[100]; 

    units[0].move = Test; 
    units[0].x = 1; 
    units[0].y = 2; 

    units[0].move(units[0], 3); 
} 

輸出:

Test1.x: 1 
Test1.y: 2 
Test2 : 3 

如果您想通過referebce傳遞結構,簡單地說:

#include <stdio.h> 

typedef struct UnitTag { 
    int x; 
    int y; 
    void (*move)(struct UnitTag*, int); 
} Unit; 

void Test (struct UnitTag *test1, int test2) 
{ 
    test1->x = 4; 
    test1->y = 5; 
} 

int main(void) 
{ 
    Unit units[100]; 

    units[0].move = Test; 
    units[0].x = 1; 
    units[0].y = 2; 

    units[0].move(&units[0], 3); 

    printf("units[0].x: %d\n", units[0].x); 
    printf("units[0].y: %d\n", units[0].y); 
} 

輸出爲:

units[0].x: 4 
units[0].y: 5 
+0

似乎工作,但是我會修改結構,我是通過引用傳遞這是。 – LamaCoder

+0

@LamaCoder我編輯。 – LPs

+0

我用代碼中的Unit替換UnitTag,並且還刪除參數中的Test關鍵字的struct關鍵字。它似乎工作正常,只是爲了提高可讀性,這是不好的做法呢? – LamaCoder

1

你使用它之前需要的原型裝置。

#include <stddef.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

typedef struct UnitTag Unit; 

typedef struct UnitTag { 
    int x; 
    int y; 
    void (*move)(Unit, int); 
} Unit; 

int main(void) 
{ 
    return 0; 
} 

之後澄清你想要做什麼。給Unit賦一個指針可能更有意義,這樣返回void的move命令可以改變你的對象。

#include <stddef.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

typedef struct UnitTag Unit; 

typedef struct UnitTag { 
    int x; 
    int y; 
    void (*move)(Unit *, int); 
} Unit; 

Unit test; 

/* some function that corresponds to the interface */ 
void myMove(Unit *u, int i) 
{ 
    u->x = u->x + i; 
} 

int main(void) 
{ 
    /* initialize test struct */ 
    test.x = 0; 
    test.y = 0; 
    test.move = myMove; 

    test.move(&test, 5); 

    printf("Values after move are (x, y) = (%i, %i).\n", test.x, test.y); 

    return 0; 
} 
+0

請告訴你如何失效(*移動)(單位);工作中? – MayurK

+0

它解決了這個錯誤,但是現在我不能使用和我以前一樣的函數,錯誤是: 錯誤:'units [i] .move' 單位的參數1的不兼容類型[i] .move &units [i],cycles); ^ 注意:期望'單位',但參數的類型是'結構UnitTag *' 現在我遇到了這個錯誤無處不在我使用這個。數組的定義是:Unit units [UNITCOUNT]; – LamaCoder

+1

您應該在您使用該結構的地方發佈代碼。 – LPs