Home > Back-end >  I am questioning how this code compiled even though struct iwreq does not have ifr_name as a member
I am questioning how this code compiled even though struct iwreq does not have ifr_name as a member

Time:10-16

I wrote very simple code using 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. And the iwreq does not ifr_name as a member.

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;
}

CodePudding user response:

"wirless.h" includes "if.h", and inside "if.h" you can find this:

#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));

CodePudding user response:

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

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

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

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