本文介绍了为什么在初始化 SDL 之前在 SDL 中声明一个指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这段代码中,我看到他们在初始化 SDL 之前声明了一个指针:

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?

习惯在int main()开头声明指针.(我也在其他源程序中看到过这种情况,来自示例程序)

The habit of declaring pointers at the beginning of int main(). (I've also seen this happen in other source programs, from example programs)

推荐答案

为什么需要在 SDL_Init 之前声明指针没有技术原因.声明一个指针变量没有任何意义,它只是在堆栈上为该指针保留空间.它可以很容易地在 SDL_Init 之后声明,或者作为调用 SDL_CreateWindow 的语句的一部分.

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.

这篇关于为什么在初始化 SDL 之前在 SDL 中声明一个指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 08:56