这两个 C 警告究竟意味着什么?
有以下 C 代码:
typedef void*(m3_func)(void);
#define NULL ((void*)0)
char* lolinfo()
{
return "You got the additional info! :D";
}
m3_func** m3_funcs() {
return (m3_func**) {
(m3_func*)(&lolinfo), // warning #1
NULL
}; // warning #2
}
我收到这些警告:
- /home/lk/proj/m3/m3_lolauncher/lolauncher.c(0,0): 警告:从不兼容的指针类型初始化 (m3_lolauncher)
- /home/lk/proj/m3/m3_lolauncher/lolauncher.c(0,0):警告:标量中的元素过多初始化程序 (m3_lolauncher)
我不明白第一个,因为我转换正确?
第二个我还没见过
have the following C code:
typedef void*(m3_func)(void);
#define NULL ((void*)0)
char* lolinfo()
{
return "You got the additional info! :D";
}
m3_func** m3_funcs() {
return (m3_func**) {
(m3_func*)(&lolinfo), // warning #1
NULL
}; // warning #2
}
I'm getting these warnings:
- /home/lk/proj/m3/m3_lolauncher/lolauncher.c(0,0): Warning: initialization from incompatible pointer type (m3_lolauncher)
- /home/lk/proj/m3/m3_lolauncher/lolauncher.c(0,0): Warning: excess elements in scalar initializer (m3_lolauncher)
I dont understand the first one as i cast correctly?
I've never seen the second one...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
列表初始化将是:
您在这里所做的是使用新的通用初始化 (
x{y}
)。因此,您尝试使用两个m3_func*
指针初始化单个m3_func**
指针。因此,您有两个警告:m3_func**
!=m3_func*
)进行初始化A list initialization would be:
What you are doing here is using the new universal initialization (
x{y}
). Hence, you're trying to initialize a singlem3_func**
pointer with twom3_func*
pointers. Ergo you have two warnings:m3_func**
!=m3_func*
)看来您的示例代码不是有效的 C。
如果我理解您的代码,则
m3_funcs()
函数应该返回一个 NULL 终止的函数指针数组。您实际上是在尝试使用初始化程序 ({...}
) 来声明一个数组并立即返回它。但我认为您不能在变量声明之外使用初始化程序...另外,请注意,此“变量”仅存在于m3_funcs()
调用的上下文中,因此地址最终可能返回的内容在函数返回后将不再有效。实现此类功能的正确方法是拥有一个静态全局变量,并返回其地址:
it seems your sample code is not valid C.
if i understand your code, the
m3_funcs()
function should return a NULL terminated array of function pointers. you are actually trying to use an initializer ({...}
) to declare an array and return it right away. but i don't think you can use an initializer outside of a variable declaration... also, note that this "variable" would exists only in the context of them3_funcs()
call, so the address that might eventually be returned would no more be valid after the function has returned.the correct way to implement such a feature is to have a static global variable, and return its address: