初始化丢弃限定符... sdl 警告
当我通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
文档说该函数返回一个
const SDL_VideoInfo *
,因此,将代码更改为:如果没有
const
,则可以使用info
更改它指向的值,但显然,您不能这样做。The documentation says that the function returns a
const SDL_VideoInfo *
, so change your code to:Without the
const
,info
could be used to change the value pointed to by it, but obviously, you can't do that.SDL_GetVideoInfo
是否可以返回指向const SDL_VideoInfo
的指针?[2分钟后谷歌搜索]
我在网上找到的
SDL_GetVideoInfo
的声明是:实际上它返回一个常量指针,您可以将其转换为非常量指针,因此警告。请注意,您不应该忽略它,因为当函数想要返回 const 指针时,它通常有充分的理由 - 通过指针修改返回的对象可能没有意义,甚至是有害的。
Could
SDL_GetVideoInfo
be returning a pointer to aconst SDL_VideoInfo
?[2 minutes and a google search later]
The declaration of
SDL_GetVideoInfo
I found online is: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.