request for member 'flags' in something not a structure or union

本文解析了一个常见的编程错误:尝试在非结构体或联合体中使用结构指针成员。该问题通常发生在试图通过点操作符访问指针所指向的结构体成员时。正确的做法应该是使用箭头操作符而非点操作符。
request for member 'flags' in something not a structure or union错误
原因:结构指针没有正确使用,如struct *p ; 应该p->flags调用,而错误的使用了p.flags 。
编译的时候报错了,该怎么修改代码解决? PATH=“/mydisk/platform/buildroot/build/armv8/host/bin:/mydisk/platform/buildroot/build/armv8/host/sbin:/usr/local/bin:/home/felix/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin” /usr/bin/make -j4 -C /mydisk/platform/buildroot/build/armv8/build/tplink/gpioReset-1.0/ Scanning dependencies of target gpioReset [ 33%] Building C object CMakeFiles/gpioReset.dir/srvcGpioReset.c.o [ 66%] Building C object CMakeFiles/gpioReset.dir/mainGpioReset.c.o srvcGpioReset.c: In function ‘gpioResetSrvc’: srvcGpioReset.c:22:16: error: ‘ERR_INIT’ undeclared (first use in this function); did you mean ‘ESRMNT’? return ERR_INIT; ^~~~~~~~ ESRMNT srvcGpioReset.c:22:16: note: each undeclared identifier is reported only once for each function it appears in srvcGpioReset.c:80:12: error: ‘ERR_NO_ERROR’ undeclared (first use in this function); did you mean ‘PAL_MQ_ERROR’? return ERR_NO_ERROR; ^~~~~~~~~~~~ PAL_MQ_ERROR srvcGpioReset.c: In function ‘gpioResetSrvcExit’: srvcGpioReset.c:86:12: error: ‘ERR_NO_ERROR’ undeclared (first use in this function); did you mean ‘PAL_MQ_ERROR’? return ERR_NO_ERROR; ^~~~~~~~~~~~ PAL_MQ_ERROR srvcGpioReset.c:87:1: error: control reaches end of non-void function [-Werror=return-type] } ^ cc1: all warnings being treated as errors CMakeFiles/gpioReset.dir/build.make:78: recipe for target ‘CMakeFiles/gpioReset.dir/srvcGpioReset.c.o’ failed make[4]: *** [CMakeFiles/gpioReset.dir/srvcGpioReset.c.o] Error 1 make[4]: *** Waiting for unfinished jobs… mainGpioReset.c: In function ‘init_gpio_reset’: mainGpioReset.c:89:5: error: unknown type name ‘shell_init_func_t’ shell_init_func_t shell_initfunc = {}; ^~~~~~~~~~~~~~~~~ mainGpioReset.c:89:40: error: empty scalar initializer shell_init_func_t shell_initfunc = {}; ^ mainGpioReset.c:89:40: note: (near initialization for ‘shell_initfunc’) mainGpioReset.c:90:28: error: request for member ‘mm_init_table’ in something not a structure or union memcpy(&(shell_initfunc.mm_init_table), &initFunc, sizeof(mm_app_init_t)); ^ mainGpioReset.c:91:5: error: implicit declaration of function ‘shell_regfunc’ [-Werror=implicit-function-declaration] shell_regfunc(MODULE_GPIO_RESET_INDEX, shell_initfunc, srvc, mm_app_srvc_size(srvc), GPIO_RESET_SUB_NAME); ^~~~~~~~~~~~~ mainGpioReset.c:91:19: error: ‘MODULE_GPIO_RESET_INDEX’ undeclared (first use in this function); did you mean ‘SRVC_GPIO_RESET_H’? shell_regfunc(MODULE_GPIO_RESET_INDEX, shell_initfunc, srvc, mm_app_srvc_size(srvc), GPIO_RESET_SUB_NAME); ^~~~~~~~~~~~~~~~~~~~~~~ SRVC_GPIO_RESET_H mainGpioReset.c:91:19: note: each undeclared identifier is reported only once for each function it appears in cc1: all warnings being treated as errors CMakeFiles/gpioReset.dir/build.make:65: recipe for target ‘CMakeFiles/gpioReset.dir/mainGpioReset.c.o’ failed make[4]: *** [CMakeFiles/gpioReset.dir/mainGpioReset.c.o] Error 1 CMakeFiles/Makefile2:78: recipe for target ‘CMakeFiles/gpioReset.dir/all’ failed make[3]: *** [CMakeFiles/gpioReset.dir/all] Error 2 Makefile:132: recipe for target ‘all’ failed make[2]: *** [all] Error 2 package/tplink/tplink-generic.mk:289: recipe for target ‘/mydisk/platform/buildroot/build/armv8/build/tplink/gpioReset-1.0/.stamp_built’ failed make[1]: *** [/mydisk/platform/buildroot/build/armv8/build/tplink/gpioReset-1.0/.stamp_built] Error 2 Makefile:84: recipe for target ‘_all’ failed make: *** [_all] Error 2 下面是现在的代码: main: /*!Copyright (c) 2010-2011 TP-LINK Technologies CO.,LTD. *All rights reserved. * *\file mainRebootScheduleCpn.c *\brief * *\author Yang Jingjing *\version *\date 2025-09-09 */ /**************************************************************************************************/ /* INCLUDE_FILES */ /**************************************************************************************************/ /* libc header */ #include <stdio.h> #include <unistd.h> #include "fepDefs.h" #include "pal/msgq.h" #include "tpDbg/tpdebug.h" #include "mm/mm.h" #include "common/applError.h" #include "srvcGpioReset.h" /**************************************************************************************************/ /* DEFINES */ /**************************************************************************************************/ #define GPIO_RESET_USR_NAME_MAX_LEN 10 #define GPIO_RESET_SUB_NAME "GPIO_RESET" /**************************************************************************************************/ /* TYPES */ /**************************************************************************************************/ /**************************************************************************************************/ /* EXTERN_PROTOTYPES */ /**************************************************************************************************/ /**************************************************************************************************/ /* LOCAL_PROTOTYPES */ /**************************************************************************************************/ /**************************************************************************************************/ /* VARIABLES */ /**************************************************************************************************/ static mm_app_prvt_srvc_t srvc[] = { { .tid = PAL_THREAD_ERROR, .func = gpioResetSrvc, .pre_exit_f = gpioResetSrvcExit, .name = "gpio-reset", .start = MM_APP_SRVC_IDLE, .stack_size = SRVC_GPIO_RESET_STACK_SIZE_DEFAULT, .prio = SRVC_GPIO_RESET_PRIO_DEFAULT, .srvc_id = GPIO_RESET_PRVT_SRVC, .meta_arg = NULL } }; /**************************************************************************************************/ /* LOCAL_FUNCTIONS */ /**************************************************************************************************/ /**************************************************************************************************/ /* PUBLIC_FUNCTIONS */ /**************************************************************************************************/ static __attribute__((constructor)) int init_gpio_reset() { mm_app_init_t initFunc = {0}; DBG("GPIO Reset main start."); // 初始化函数表(保留基本结构,简化不必要的初始化) initFunc._init_platform = NULL; initFunc._init_private = NULL; initFunc._init_public = NULL; initFunc._init_application = NULL; initFunc._init_preconfig = NULL; initFunc._init_defconfig = NULL; initFunc._init_loadconfig = NULL; initFunc._init_stable = NULL; shell_init_func_t shell_initfunc = {}; memcpy(&(shell_initfunc.mm_init_table), &initFunc, sizeof(mm_app_init_t)); shell_regfunc(MODULE_GPIO_RESET_INDEX, shell_initfunc, srvc, mm_app_srvc_size(srvc), GPIO_RESET_SUB_NAME); DBG("GPIO Reset main loop."); return 0; } srvc: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/socket.h> #include <linux/netlink.h> #include "srvcGpioReset.h" #include "tpDbg/tpdebug.h" int gpioResetSrvc(void *param) { struct sockaddr_nl src_addr, dest_addr; struct nlmsghdr *nlh = NULL; struct iovec iov; struct msghdr msg; int sock_fd, ret; // 创建Netlink socket sock_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GPIO_EVENT); if (sock_fd < 0) { DBG_ERR("Failed to create netlink socket: %s", strerror(errno)); return ERR_INIT; } memset(&src_addr, 0, sizeof(src_addr)); src_addr.nl_family = AF_NETLINK; src_addr.nl_pid = getpid(); // 应用进程ID src_addr.nl_groups = 1; // 加入组播组1 // 绑定socket if (bind(sock_fd, (struct sockaddr*)&src_addr, sizeof(src_addr))) { DBG_ERR("Bind failed: %s", strerror(errno)); close(sock_fd); return ERR_INIT; } // 准备接收缓冲区 nlh = (struct nlmsghdr *)malloc(NLMSG_SPACE(sizeof(struct gpio_netlink_msg))); if (!nlh) { DBG_ERR("Memory allocation failed"); close(sock_fd); return ERR_INIT; } memset(nlh, 0, NLMSG_SPACE(sizeof(struct gpio_netlink_msg))); iov.iov_base = (void *)nlh; iov.iov_len = NLMSG_SPACE(sizeof(struct gpio_netlink_msg)); memset(&msg, 0, sizeof(msg)); msg.msg_name = (void *)&dest_addr; msg.msg_namelen = sizeof(dest_addr); msg.msg_iov = &iov; msg.msg_iovlen = 1; DBG("GPIO Reset service started. Waiting for reset events..."); while (1) { // 接收Netlink消息 ret = recvmsg(sock_fd, &msg, 0); if (ret < 0) { DBG_ERR("recvmsg error: %s", strerror(errno)); continue; } // 解析消息 struct gpio_netlink_msg *msg_data = (struct gpio_netlink_msg *)NLMSG_DATA(nlh); // 检查是否为reset事件 if (strcmp(msg_data->button_name, "reset") == 0) { DBG("=== RESET EVENT DETECTED ==="); DBG("Event type: %u", msg_data->event_type); DBG("Value: %d", msg_data->value); DBG("Timestamp: %lu", msg_data->timestamp); DBG("============================"); } } free(nlh); close(sock_fd); return ERR_NO_ERROR; } int gpioResetSrvcExit(void *param) { // 清理资源的位置(实际在循环中不会退出) return ERR_NO_ERROR; } srvc.h: #ifndef _SRVC_GPIO_RESET_H_ #define _SRVC_GPIO_RESET_H_ #ifdef __cplusplus extern "C" { #endif #include <linux/netlink.h> #include <sys/socket.h> // 与内核相同的定义 #define NETLINK_GPIO_EVENT 31 #define SRVC_GPIO_RESET_STACK_SIZE_DEFAULT 10240 #define SRVC_GPIO_RESET_PRIO_DEFAULT PAL_THREAD_PRIO_IGNORE #define GPIO_RESET_PRVT_SRVC 0 // Netlink消息结构(与内核保持一致) struct gpio_netlink_msg { char button_name[32]; unsigned int event_type; int value; unsigned long timestamp; }; int gpioResetSrvc(void *param); int gpioResetSrvcExit(void *param); #ifdef __cplusplus } #endif #endif // _SRVC_GPIO_RESET_H_
10-14
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值