这两个 C 警告究竟意味着什么?

发布于 2024-12-02 06:36:38 字数 568 浏览 2 评论 0原文

有以下 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 技术交流群。

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

发布评论

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

评论(2

白云不回头 2024-12-09 06:36:39

列表初始化将是:

a = { b,c,d }

您在这里所做的是使用新的通用初始化 (x{y})。因此,您尝试使用两个 m3_func* 指针初始化单个 m3_func** 指针。因此,您有两个警告:

  1. 从不兼容的指针类型( m3_func** != m3_func* )进行初始化
  2. 标量初始值设定项中的多余元素(指针是标量,并且您尝试用两个指针初始化它——因此一个指针过多)

A list initialization would be:

a = { b,c,d }

What you are doing here is using the new universal initialization (x{y}). Hence, you're trying to initialize a single m3_func** pointer with two m3_func* pointers. Ergo you have two warnings:

  1. initialization from incompatible pointer type ( m3_func** != m3_func* )
  2. excess elements in scalar initializer ( a pointer is a scalar, and you're trying to initialize it with two pointers -- ergo one excessive )
韶华倾负 2024-12-09 06:36:38

看来您的示例代码不是有效的 C。

如果我理解您的代码,则 m3_funcs() 函数应该返回一个 NULL 终止的函数指针数组。您实际上是在尝试使用初始化程序 ({...}) 来声明一个数组并立即返回它。但我认为您不能在变量声明之外使用初始化程序...另外,请注意,此“变量”仅存在于 m3_funcs() 调用的上下文中,因此地址最终可能返回的内容在函数返回后将不再有效。

实现此类功能的正确方法是拥有一个静态全局变量,并返回其地址:

static m3_func *m3_funcs_array[] = {(m3_func *)&lolinfo, NULL};

m3_func ** m3_funcs()
{
    return &m3_funcs_array;
}

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 the m3_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:

static m3_func *m3_funcs_array[] = {(m3_func *)&lolinfo, NULL};

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