I have the following code that gets the MAC address of an interface:
static struct ifreq get_mac( int socket_desc, char *if_name ) {
struct ifreq if_mac;
memset( &if_mac, 0, sizeof( struct ifreq ) );
strncpy( if_mac.ifr_name, if_name, IFNAMSIZ - 1 );
...
return if_mac;
}
My C experience is limited to what I did in college. I roughly get pointers but I understand that returning large structs by value is a bad idea because you can run out of stack space (stackoverflow!). How can I change the above code to return a pointer to if_mac instead ? It’s just a bit confusing because there’s a struct and an ‘address of’ operator :S.
Give a pointer to the expected structure in parameter:
If the caller has already allocated the space for the structure, the function prototype becomes: