gpt4 book ai didi

c - 如何忽略警告 "excess elements in struct initializer"

转载 作者:太空宇宙 更新时间:2023-11-03 23:26:49 26 4
gpt4 key购买 nike

我想在 c(gcc) 中使用关键字参数,我遵循 this link

如下所示:

#include <stdio.h>      // printf
#include <stdbool.h> // bool - there's a bool type now

// Macro that turns the kwargs into an struct
#define my_func(...) my_func_base(\
(struct user){.is_admin=false, __VA_ARGS__});

struct user {
char *username;
bool is_admin;
};

// The actual function - and yes there's single line comments too
void my_func_base(struct user u) {
printf("Hello %s\n", u.username);
}

int main(int argc, char *argv[]) {
my_func(.username="Bob", .is_admin=true);
}

我可以很好地调用这个函数

my_func(.username="Peter");
my_func(.username="Bob", .is_admin=true);

它会预处理

my_func(.is_admin=false, .username="Bob", .is_admin=true); ///<is_admin is overwritten

当我编译代码时,我收到如下警告:

(near initialization for '(anonymous)')
warning: excess elements in struct initializer
(near initialization for '(anonymous)')
warning: initialized field overwritten

我真的想覆盖它。我怎么能忽略它。使用诸如#pragma GCC diagnostic之类的东西。我搜索GNU Warning Optings ,-Woverride-init/-Wno-override-init 不工作。

我的gcc版本是

zodiac1111@debian:src_linux$ LC_ALL=en_US arm-linux-gcc -v
Using built-in specs.
Target: arm-unknown-linux-uclibcgnueabi
Configured with: /home/ldsh/rt9x5/linux/buildroot/buildroot/output/toolchain/gcc-4.3.5/configure --prefix=/opt/rt9x5/arm-linux-uclibcgnueabi/usr --build=i686-pc-linux-gnu --host=i686-pc-linux-gnu --target=arm-unknown-linux-uclibcgnueabi --enable-languages=c,c++ --with-sysroot=/opt/rt9x5/arm-linux-uclibcgnueabi/usr/arm-unknown-linux-uclibcgnueabi/sysroot --with-build-time-tools=/opt/rt9x5/arm-linux-uclibcgnueabi/usr/arm-unknown-linux-uclibcgnueabi/bin --disable-__cxa_atexit --enable-target-optspace --disable-libgomp --with-gnu-ld --disable-libssp --disable-multilib --disable-tls --enable-shared --with-gmp=/opt/rt9x5/arm-linux-uclibcgnueabi/usr --with-mpfr=/opt/rt9x5/arm-linux-uclibcgnueabi/usr --enable-threads --disable-decimal-float --with-float=soft --with-abi=aapcs-linux --with-arch=armv5te --with-tune=arm926ej-s --with-pkgversion='Buildroot 2011.05-dirty' --with-bugurl=http://bugs.buildroot.net/ : (reconfigured) /home/ldsh/rt9x5/linux/buildroot/buildroot/output/toolchain/gcc-4.3.5/configure --prefix=/opt/rt9x5/arm-linux-uclibcgnueabi/usr --build=i686-pc-linux-gnu --host=i686-pc-linux-gnu --target=arm-unknown-linux-uclibcgnueabi --enable-languages=c,c++ --with-sysroot=/opt/rt9x5/arm-linux-uclibcgnueabi/usr/arm-unknown-linux-uclibcgnueabi/sysroot --with-build-time-tools=/opt/rt9x5/arm-linux-uclibcgnueabi/usr/arm-unknown-linux-uclibcgnueabi/bin --disable-__cxa_atexit --enable-target-optspace --disable-libgomp --with-gnu-ld --disable-libssp --disable-multilib --disable-tls --enable-shared --with-gmp=/opt/rt9x5/arm-linux-uclibcgnueabi/usr --with-mpfr=/opt/rt9x5/arm-linux-uclibcgnueabi/usr --enable-threads --disable-decimal-float --with-float=soft --with-abi=aapcs-linux --with-arch=armv5te --with-tune=arm926ej-s --with-pkgversion='Buildroot 2011.05-dirty' --with-bugurl=http://bugs.buildroot.net/
Thread model: posix
gcc version 4.3.5 (Buildroot 2011.05-dirty)

最佳答案

抑制此警告的正确选项是-Wno-override-init,例如:

gcc -pedantic -Wall -Wextra -Wno-override-init check.c

来自 man gcc(4.4.7,强调我的):

-Woverride-init (C and Objective-C only)

Warn if an initialized field without side effects is overridden when using designated initializers.

This warning is included in -Wextra. To get other -Wextra warnings without this one, use -Wextra -Wno-override-init.

请注意,这也会影响数组的指定初始值设定项。

关于c - 如何忽略警告 "excess elements in struct initializer",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25156874/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com