Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8086855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:34:43+00:00 2026-06-05T18:34:43+00:00

I have the following code that gets the MAC address of an interface: static

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T18:34:45+00:00Added an answer on June 5, 2026 at 6:34 pm

    Give a pointer to the expected structure in parameter:

    static int ifreq get_mac(int socket_desc, char *if_name, struct ifreq **if_mac)
    {
          if (NULL == (*ifmac = malloc(sizeof (struct ifreq))) {
              return -1;                                        
          }                                                                                                                                                                                                                                                                                                                            
          memset(*if_mac, 0, sizeof(struct ifreq));                                                                                                                                                                                       
          strncpy((*if_mac)->ifr_name, if_name, IFNAMSIZ - 1);                                                                                                                                                                                  
          ...                                                                                                                                                                                                                                  
          return 0;                                                                                                                                                                                                                      
    }
    

    If the caller has already allocated the space for the structure, the function prototype becomes:

    static int ifreq get_mac(int socket_desc, char *if_name, struct ifreq *if_mac)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code that gets initialized as a static variable in a
In PHP I have the following code that gets a file submitted through CGI:
I have the following php code that gets a serialized array from a mysql
I have the following code in a Runnable that gets passed to a thread
I have a line of code that gets the following error when run through
I have the following code that gets called after the view loads. It works
Suppose you have the following code: int main(int argc, char** argv) { Foo f;
I have the following code that gets the value of a drop down list
I have the following question: I got some code that gets called when an
I have the following C code: static void* heap; static unsigned int ptr; int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.