Pass char pointer to function in C

Tiktac

I am trying to get Mac address by this code:

void getMacAdress(unsigned char **address)
{
    int s;
    struct ifreq buffer;

    s = socket(PF_INET, SOCK_DGRAM, 0);
    memset(&buffer, 0x00, sizeof(buffer));
    strcpy(buffer.ifr_name, "eth0");
    ioctl(s, SIOCGIFHWADDR, &buffer);
    close(s);
    *address = (unsigned char *)buffer.ifr_hwaddr.sa_data;

    for (s = 0; s < 6; s++)
    {
        printf("%.2X ", *(*address + s));
    }

    printf("\n");
}

int main(int argc, char *argv[])
{
    unsigned char *address;

    getMacAdress(&address);
    int i;

    for (i = 0; i < 6; i++)
    {
        printf("%.2X ", *(address + i));
    }

    printf("\n");
    return 0;
}

I got a correct result as

08 00 27 0A 4E 98 
08 00 27 0A 4E 98

but when I delete printf snippet code in getMacAddress() function it becomes:

void getMacAdress(unsigned char **address)
{
    int s;
    struct ifreq buffer;

    s = socket(PF_INET, SOCK_DGRAM, 0);
    memset(&buffer, 0x00, sizeof(buffer));
    strcpy(buffer.ifr_name, "eth0");
    ioctl(s, SIOCGIFHWADDR, &buffer);
    close(s);
    *address = (unsigned char *)buffer.ifr_hwaddr.sa_data;
    printf("\n");
}

I got the wrong result

08 00 00 00 00 00

Can you explain to me why that is and how I can solve this problem?

Will

you cannot point to a stack space to return in a function.

Instead, you can malloc a heap space to store the result you wanted:

void getMacAdress(unsigned char **address)
{
    int s;
    struct ifreq buffer;

    s = socket(PF_INET, SOCK_DGRAM, 0);
    memset(&buffer, 0x00, sizeof(buffer));
    strcpy(buffer.ifr_name, "eth0");
    ioctl(s, SIOCGIFHWADDR, &buffer);
    close(s);
    *address = (unsigned char*) malloc(sizeof(buffer.ifr_hwaddr.sa_data));
    memcpy(*address, buffer.ifr_hwaddr.sa_data,sizeof(buffer.ifr_hwaddr.sa_data));

    //for (s = 0; s < 6; s++)
    //{
    //    printf("%.2X ", *(*address + s));
    //}

    //printf("\n");
}

BTW, don't forget to free the heap space in your main function.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pass char pointer array to function

From Dev

Pass char pointer/array to a function

From Dev

pass a void pointer to a function with const char * const

From Dev

Alternative way to pass a char pointer to a function

From Dev

pass a void pointer to a function with const char * const

From Dev

C pass pointer as argument in function

From Dev

C returning const char pointer or char pointer from a function

From Dev

Passing a pointer to a char array as an argument to a function - C

From Dev

Passing a pointer to a char array as an argument to a function - C

From Dev

How to pass a float array to the function that has unsigned char pointer as an argument?

From Dev

Pass a function-pointer name to C Preprocessor

From Dev

Cannot pass struct pointer as function argument in C

From Dev

Pass a pointer of an array to a function to use in printf` in C

From Dev

How to pass char pointer to C++ API from python?

From Dev

How to pass an array of char's to a function in C

From Dev

How to pass in function a multidimensional char array in C?

From Dev

How to pass in function a multidimensional char array in C?

From Dev

pass and modify char array[][] in function C

From Dev

char* pointer will not increment in function

From Dev

pass "pointer to pointer" to a template function

From Dev

C *char pointer to char array

From Dev

C *char pointer to char array

From Dev

Storing address of char pointer in a char pointer in C

From Dev

Does a function's pointer to char needs memory allocation in C

From Dev

Passing char pointer to another function: blank value. c++

From Dev

C Programming: Initialize pointer to char array inside a function

From Dev

Does a function's pointer to char needs memory allocation in C

From Dev

ANSI C - passing data from function to a CHAR pointer

From Dev

C Program: Update unsigned char pointer array with function

Related Related

  1. 1

    Pass char pointer array to function

  2. 2

    Pass char pointer/array to a function

  3. 3

    pass a void pointer to a function with const char * const

  4. 4

    Alternative way to pass a char pointer to a function

  5. 5

    pass a void pointer to a function with const char * const

  6. 6

    C pass pointer as argument in function

  7. 7

    C returning const char pointer or char pointer from a function

  8. 8

    Passing a pointer to a char array as an argument to a function - C

  9. 9

    Passing a pointer to a char array as an argument to a function - C

  10. 10

    How to pass a float array to the function that has unsigned char pointer as an argument?

  11. 11

    Pass a function-pointer name to C Preprocessor

  12. 12

    Cannot pass struct pointer as function argument in C

  13. 13

    Pass a pointer of an array to a function to use in printf` in C

  14. 14

    How to pass char pointer to C++ API from python?

  15. 15

    How to pass an array of char's to a function in C

  16. 16

    How to pass in function a multidimensional char array in C?

  17. 17

    How to pass in function a multidimensional char array in C?

  18. 18

    pass and modify char array[][] in function C

  19. 19

    char* pointer will not increment in function

  20. 20

    pass "pointer to pointer" to a template function

  21. 21

    C *char pointer to char array

  22. 22

    C *char pointer to char array

  23. 23

    Storing address of char pointer in a char pointer in C

  24. 24

    Does a function's pointer to char needs memory allocation in C

  25. 25

    Passing char pointer to another function: blank value. c++

  26. 26

    C Programming: Initialize pointer to char array inside a function

  27. 27

    Does a function's pointer to char needs memory allocation in C

  28. 28

    ANSI C - passing data from function to a CHAR pointer

  29. 29

    C Program: Update unsigned char pointer array with function

HotTag

Archive