问题
In this code, I see that they declare a pointer before initializing SDL:
int main(int argc, char* argv[]) {
SDL_Window *window; // Declare a pointer
SDL_Init(SDL_INIT_VIDEO); // Initialize SDL2
// Create an application window with the following settings:
window = SDL_CreateWindow(
"An SDL2 window", // window title
SDL_WINDOWPOS_UNDEFINED, // initial x position
SDL_WINDOWPOS_UNDEFINED, // initial y position
640, // width, in pixels
480, // height, in pixels
SDL_WINDOW_OPENGL // flags - see below
);
(full code can be found here)
Wouldn't it be more organized to declare the pointer right before you create a window, just so it would be neater and more organized? Why declare it beforehand?
If I would take a guess, it's good to just have all the pointers in one area, so you can see all the pointers at one time. Or is it just a good habit to get used to?
The habit of declaring pointers at the beginning of int main()
. (I've also seen this happen in other source programs, from example programs)
回答1:
There is no technical reason why you need to declare a pointer before SDL_Init. Declaring a pointer variable carries no implications with it, it just reserves space on the stack for that pointer. It could just as easily be declared after SDL_Init, or as part of the statement that calls SDL_CreateWindow.
I honestly don't know why they put it that way in the docs.
来源:https://stackoverflow.com/questions/51296729/why-declare-a-pointer-in-sdl-before-initializing-sdl