简体   繁体   English

即使 struct iwreq 没有​​ ifr_name 作为成员,我也在质疑这段代码是如何编译的

[英]I am questioning how this code compiled even though struct iwreq does not have ifr_name as a member

I wrote very simple code using struct iwreq.我使用 struct iwreq 编写了非常简单的代码。 Also, I expected this will be error.另外,我预计这将是错误的。 But it is compiled and works.但它已编译并有效。

I looked inside linux/wireless.h which has the definition of struct iwreq.我查看了 linux/wireless.h,里面有 struct iwreq 的定义。 And the iwreq does not ifr_name as a member.并且 iwreq 没有​​将 ifr_name 作为成员。

Would someone can give me an idea?有人可以给我一个想法吗? Here is the simple code.这是简单的代码。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <linux/wireless.h>

int main(void)
{
   char *intf = "eth0";
   struct iwreq iwr;

   strncpy(iwr.ifr_name, intf, sizeof(iwr.ifr_name));

   printf("main : intf = %s, iwr.ifr_name = %s\n", intf, iwr.ifr_name);
   return 0;
}

wireless.h includes if.h , and inside if.h you can find: wireless.h包括if.h ,在if.h你可以找到:

#define ifr_name    ifr_ifrn.ifrn_name  /* interface name   */

So the code is translated to:所以代码被翻译成:

strncpy(iwr.ifr_ifrn.ifrn_name, intf, sizeof(iwr.ifr_ifrn.ifrn_name));

/usr/include/linux/wireless.h includes linux/if.h: /usr/include/linux/wireless.h 包含 linux/if.h:

#include <linux/if.h>           /* for IFNAMSIZ and co... */

And in /usr/include/linux/if.h there is:在 /usr/include/linux/if.h 中有:

#define ifr_name    ifr_ifrn.ifrn_name  /* interface name   */

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我收到一个“错误:重新定义”符号,即使我之前在文件中使用了相同的语法, - I am getting an "error: redefinition of" symbol, even though I have used the same syntax earlier in the file, 如何基于成员名称在C中初始化结构 - How do I initialize a struct in C based on the member's name 我有一个返回结构的char *成员的函数。 我正在尝试调用该函数并为其分配一个字符串,但出现错误 - I have a function that's returning a char* member of a struct. I am trying to call that function and assign a string to it, but am getting an error 即使代码在逻辑上不正确,为什么我得到正确的输出 - Why am I getting correct output even though the code is logically incorrect 具有单个成员的结构是否具有与成员类型相同的性能? - Does struct with a single member have the same performance as a member type? 为什么这个代码仍然打印 1,即使我已经添加了项目? - Why does this code still print 1, even though I already add item? 即使我输入了一个数字,为什么我的代码仍然返回 1 和错误消息? - Why does my code keep returning 1 with the error message even though I input a digit? 即使我使用的是 malloc,strcpy 上的分段错误 - Segmentation fault on strcpy even though I am using malloc 为什么我的代码即使正确也不起作用? - Why does my code does not work even though it is correct? 为什么arm-none-eabi-size报告.data部分为0,即使我使用初始化的RAM? - Why does arm-none-eabi-size report the .data section to be 0 even though I am using initialized RAM?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM