初始化丢弃限定符... sdl 警告

发布于 2024-09-24 14:05:23 字数 534 浏览 1 评论 0原文

当我通过 GCC 运行这段代码时,我在将信息设置为 SDL_GetVideoInfo() 的行上收到此警告。

警告:初始化丢弃指针目标类型中的限定符

int main(int argc, char** argv) {
    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_VideoInfo* info = SDL_GetVideoInfo();
    int SCREEN_WIDTH = info->current_w;
    int SCREEN_HEIGHT = info->current_h;
    printf("hardware acceleration? %s\n", info->hw_available == 1 ? "yes" : "no");
    printf("memory available in kilobytes: %d\n", info->video_mem);
    SDL_Quit();
    return 0;
}

有谁知道如何更改代码以便绕过该警告?

When I run this bit of code through GCC I get this warning on the line where I set info to SDL_GetVideoInfo().

warning: initialization discards qualifiers from pointer target type

int main(int argc, char** argv) {
    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_VideoInfo* info = SDL_GetVideoInfo();
    int SCREEN_WIDTH = info->current_w;
    int SCREEN_HEIGHT = info->current_h;
    printf("hardware acceleration? %s\n", info->hw_available == 1 ? "yes" : "no");
    printf("memory available in kilobytes: %d\n", info->video_mem);
    SDL_Quit();
    return 0;
}

Does anyone know how I can change the code so I can get around that warning?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑叹一世浮沉 2024-10-01 14:05:23

文档说该函数返回一个const SDL_VideoInfo *,因此,将代码更改为:

const SDL_VideoInfo* info = SDL_GetVideoInfo();

如果没有 const,则可以使用 info 更改它指向的值,但显然,您不能这样做。

The documentation says that the function returns a const SDL_VideoInfo *, so change your code to:

const SDL_VideoInfo* info = SDL_GetVideoInfo();

Without the const, info could be used to change the value pointed to by it, but obviously, you can't do that.

花落人断肠 2024-10-01 14:05:23

SDL_GetVideoInfo 是否可以返回指向 const SDL_VideoInfo 的指针?


[2分钟后谷歌搜索]

我在网上找到的SDL_GetVideoInfo的声明是:

const SDL_VideoInfo* SDL_GetVideoInfo(void);

实际上它返回一个常量指针,您可以将其转换为非常量指针,因此警告。请注意,您不应该忽略它,因为当函数想要返回 const 指针时,它通常有充分的理由 - 通过指针修改返回的对象可能没有意义,甚至是有害的。

Could SDL_GetVideoInfo be returning a pointer to a const SDL_VideoInfo ?


[2 minutes and a google search later]

The declaration of SDL_GetVideoInfo I found online is:

const SDL_VideoInfo* SDL_GetVideoInfo(void);

Indeed it returns a const pointer which you convert to a non-const pointer, hence the warning. Note that you shouldn't just ignore it, since when the function wants to return a const pointer it frequently has a good reason for it - it probably doesn't make sense, or is even harmful, to modify the returned object through the pointer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文