[英]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.