2011-02-25 111 views
0

具有下面的代碼,我怎麼能有「得」收到一張地圖,並在指定的位置返回值?C傳遞函數指針,如何?

我試圖用C編寫元胞自動機,試圖環繞指針和內存分配我的頭。一切都很好,直到我決定做「得」來獲取數據,而不是直接映射[X + world.w * Y]爲我所用。 我需要這個,因爲在未來,我計劃有兩個相同尺寸的地圖,並使用相同的功能,從他們那裏得到的數據(所以它會是「得(& MAP2,X,Y)」,而不是「得到(&圖,X,Y)」。

我這樣做是因爲我是反對使用全局變量建議,所以我會繼續在主要的兩個地圖和發送它們的地址給函數來處理。

這是C語言,所以沒有C++的解決方案是有效的。

我試着在谷歌搜索這一點,但所有的文檔都非常的技術和令人費解的,我不知道這個過程實際上是如何命名...那麼,有誰能幫助我呢?我如何將一個malloc'ed數組傳遞給一個函數並從中檢索或更改數據?

typedef struct Map { 
int HP; 
int type; 
unsigned int flags; 
} Map; 

typedef struct World { 
int w; 
int h; 
} World; 

struct World world; 

int tile (int x, int y) { return x + world.w * y; } 

int get (/*unknown*/map , int x, int y){ 
int val = x + world.w * y; 
return /*unknown ->?*/ type; 
} 


int main(){ 
Map* map; 
world.w = 8; 
world.h = 8; 
int tiles = world.w * world.h; 
map = (Map*)malloc(sizeof(Map) * tiles); 
int i; 
for(i = 0; i < tiles; i++){ 
    map[i].type = rand()%2; 
} 
int x,y; 

while(1){ 
    put(0,0); 
    for(y = 0; y < world.h; y++){ 
     printf("\n"); 
     for(x = 0; x < world.w; x++){ 
      printf("%i ", get(&map, x, y)); 
     } 
    } 
}; 
printf("\n"); 
return 0; 

}

+0

我認爲你需要闡明'Map's和'Worlds'之間的關係。除非我誤解了某些東西,否則它不會*看起來像你有一個問題,指向函數的指針將很好地解決...... –

+0

@Jerry:我不明白這是一個問題。世界定義了地圖大小(稍後將用其他變量進行擴展),Map是地圖數據。 – viktorry

回答

2

相反的:

get(&map, x, y)

在其中通過地址地圖指針的malloc的地址()返回,你只需要把這個地址本身:從您的代碼

get(map, x, y)
AFAICT,malloc()函數返回恰好正在尋找,即一個指向內存中的某個地方有空間爲64瓦那得到的東西()。所以get()可能看起來像這樣:
int 
get(Map *map, int x, inty ) { 
    int val = x + map->w * y; // map is a pointer to struct, not the struct itself 
    return val;    // get() returns an int, and it's in val 
}

這可能更接近你想要的。

- 皮特

有你的代碼的一些其他錯誤,太多,但是這可能讓編譯器功成。

+0

正是我需要的。現在我終於明白了,做了一個getter和setter http://paste.ubuntu.com/572308/。我可以繼續使用這個自動機的代碼。 – viktorry

+0

順便說一下,您是否在那裏看到更重要的錯誤?編譯器現在什麼都不輸出。 – viktorry

+0

不相干,但我喜歡隨便使用「AFAICT」:) – tekknolagi

1

你在main()map變量已經是一個Map *所以不要在調用點底肥&它出來的一個雙重間接指針。

此外,int get(Map *m, int x, int y) { ... return map[...]; }

不要malloc(3)將返回值。

+0

這是否與上面的typedef中的類型變量一起使用?如返回地圖[---] .type?編輯:似乎它!這似乎工作。但是,我何時需要真正通過&?外行的條件有什麼區別? – viktorry

+0

想象一下'x'不是一個指針,比如說它是一個int,double或struct。現在'&x'給你一個'int *','double *'或'struct *'。爲了保持生活有趣,這會自動發生在數組中,不需要&操作符。 – DigitalRoss

0

我可能是錯的,但從閱讀你的問題,它聽起來並不像你真的在尋找一個函數指針,而是一個指向Map結構的指針傳遞給一個函數。也許你想要返回一個指向特定Map元素的指針。如果是這樣的情況下,它看起來像下面這樣:

typedef struct Map { 
int HP; 
int type; 
unsigned int flags; 
} Map; 

typedef struct World { 
int w; 
int h; 
} World; 

struct World world; 

int tile (int x, int y) { return x + world.w * y; } 

Map * get (Map *map , int x, int y){ 
return map[x + world.w * y]; 
} 


int main(){ 
Map *map; 
Map *m; 
world.w = 8; 
world.h = 8; 
int tiles = world.w * world.h; 
map = (Map*)malloc(sizeof(Map) * tiles); 
int i; 
for(i = 0; i < tiles; i++){ 
    map[i].type = rand()%2; 
} 
int x,y; 

while(1){ 
    put(0,0); 
    for(y = 0; y < world.h; y++){ 
     printf("\n"); 
     for(x = 0; x < world.w; x++){ 
      m = get(map, x, y); // map was already declared as a pointer 
      printf("%d\n", m->HP); 
      printf("%d\n", m->type); 
      printf("%X\n", m->flags); 
     } 
    } 
}; 
printf("\n"); 
return 0; 
0

傳遞一個指針在C結構(或聯合)很簡單:

typedef struct Foo 
{ 
    int a  ; 
    char b[32] ; 
} FOO ; 

void do_something_with_a_foo(FOO* p) 
{ 
    // using the arror operation '->' to deference a structure pointer 
    int m = p->a ; 
    char n = p->b[3] ; 

    // using the * operator to deference a structure pointer 
    int x = (*p).a ; 
    char y = (*p).b[3] ; 

    return ; 
} 

void pass_a_foo_pointer_to_function() 
{ 
    FOO stack_instance = { 3 , "hello, world" , } ; 
    FOO* heap_instance = malloc(sizeof(FOO)) ; 

    heap_instance->a = 12 ; 
    strcpy(heap_instance->b , "this, that and the other") ; 

    do_something_with_a_foo(&stack_instance) ; 
    do_something_with_a_foo( heap_instance ) ; 

    free(heap_instance) ; 

    return ; 
} 

乾杯!