C通过指针返回结构
C returning structs via pointer
我目前正在开始使用 c 和 allegro5。我想将我的初始化代码放入 init() 方法中,但我正在努力 return 指向 ALLEGRO_EVENT_QUEUE 和 ALLEGRO_DISPLAY 的指针。尽管在函数中初始化了指针,但之后它们仍然是 NULL;我认为分配给指针的值会在当前范围结束后继续存在,因为基础值已修改,但内存地址保持不变。
这是我的代码:
#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;
}
非常感谢任何提示或帮助。
你需要传递指针本身的地址。
像这样尝试
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();
/* I assume you are guaranteed to not recieve `NULL's or why don't you check ? */
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();
}
和
init(&display, &event_queue);
请记住,在 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;
}
我目前正在开始使用 c 和 allegro5。我想将我的初始化代码放入 init() 方法中,但我正在努力 return 指向 ALLEGRO_EVENT_QUEUE 和 ALLEGRO_DISPLAY 的指针。尽管在函数中初始化了指针,但之后它们仍然是 NULL;我认为分配给指针的值会在当前范围结束后继续存在,因为基础值已修改,但内存地址保持不变。 这是我的代码:
#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;
}
非常感谢任何提示或帮助。
你需要传递指针本身的地址。
像这样尝试
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();
/* I assume you are guaranteed to not recieve `NULL's or why don't you check ? */
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();
}
和
init(&display, &event_queue);
请记住,在 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;
}