我目前開始與c和allegro5一起工作。我想把我的初始化代碼放到一個init()方法中,但是我正在努力將指針返回到ALLEGRO_EVENT_QUEUE和ALLEGRO_DISPLAY。儘管指針在函數中被初始化,但它們在之後仍爲NULL;我認爲分配給指針的值將在當前範圍的末尾存活,因爲底層值被修改,但內存地址保持不變。 這裏是我的代碼:C通過指針返回結構體
#include <stdio.h>
#include <allegro5/allegro.h>
void init(ALLEGRO_DISPLAY *display_ptr, ALLEGRO_EVENT_QUEUE *queue_ptr) {
al_init();
al_install_keyboard();
display_ptr = al_create_display(640, 480);
queue_ptr = al_create_event_queue();
al_register_event_source(queue_ptr, al_get_display_event_source(display_ptr));
al_clear_to_color(al_map_rgb(0,0,0));
al_flip_display();
}
int main(int argc, char **argv){
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
init(display, event_queue);
printf("%d\n", display == NULL); //prints out 1
printf("%d\n", event_queue == NULL); //prints out 1
//Further code
return 0;
}
任何提示或幫助是極大的讚賞。
耶!有用!非常感謝你。 –
只要你能夠接受答案。 –