Pass char pointer to C function


tiktak

I am trying to get the mac address with the following 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 the correct result

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

But when I remove the printfcode snippet in the function getMacAddress()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 get wrong results

08 00 00 00 00 00

Can you explain to me why, and how to fix this problem?

will

You cannot point to stack space to be returned in a function.

Instead, you can allocate a heap space to store the desired result:

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

By the way, don't forget to free the heap space in the main function.

Related


Pass char pointer to C function

tiktak I am trying to get the mac address with the following 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,

Pass char pointer to C function

tiktak I am trying to get the mac address with the following 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,

Pass pointer to char array as parameter to function - C

Macaque In the function declaration below, the first parameter is a String, specifically an array of chars, and the third parameter is a pointer to an integer. Is the second parameter a pointer to an array of characters? In other words, a pointer to a pointer?

Pass pointer to char array as parameter to function - C

Macaque In the function declaration below, the first parameter is a String, specifically an array of chars, and the third parameter is a pointer to an integer. Is the second parameter a pointer to an array of characters? In other words, a pointer to a pointer?

Pass pointer to char array as parameter to function - C

Macaque In the function declaration below, the first parameter is a String, specifically an array of chars, and the third parameter is a pointer to an integer. Is the second parameter a pointer to an array of characters? In other words, a pointer to a pointer?

Pass pointer to char array as parameter to function - C

Macaque In the function declaration below, the first parameter is a String, specifically an array of chars, and the third parameter is a pointer to an integer. Is the second parameter a pointer to an array of characters? In other words, a pointer to a pointer?

Pass pointer to char array to function

bob jane Below is my function that takes a char array of size 2, passes it to the function, and the function should return the same 2 chars (it's a bit complicated when it comes to communicating with hardware devices). The problem is when I pass char(*in)[2] t

Pass char pointer/array to function

open I'm trying to understand more about char pointers in C, but one thing got me. Suppose I want to pass a char pointer to a function and change the value represented by that pointer. Here is an example: int Foo (char *(&Msg1), char* Msg2, char* Msg3){ ch

Pass char pointer as parameter to function

unlimited This item table prints different permutations of strings. If I declare the string as a char array in main and pass the array name in the printAnagram function it works fine. But if I declare string as char *s="hello" and pass 's' it crashes. Why? #in

Pass char pointer/array to function

open I'm trying to understand more about char pointers in C, but one thing got me. Suppose I want to pass a char pointer to a function and change the value represented by that pointer. Here is an example: int Foo (char *(&Msg1), char* Msg2, char* Msg3){ ch

Pass char pointer as parameter to function

unlimited This item table prints different permutations of strings. If I declare the string as a char array in main and pass the array name in the printAnagram function it works fine. But if I declare string as char *s="hello" and pass 's' it crashes. Why? #in

Pass a char array to a function that takes a char pointer

Bruno Ely In C, why can I pass a character array to a function that takes a char *as a parameter, but not the address of the array to a function that takes a char **? UPDATE: Interestingly, changing the parameter type char* qux[12]to does n't completely change

Pass a char array to a function that takes a char pointer

Bruno Ely In C, why can I pass a character array to a function that takes a char *as a parameter, but not the address of the array to a function that takes a char **? UPDATE: Interestingly, changing the parameter type char* qux[12]to does n't completely change

Pass a char array to a function that takes a char pointer

Bruno Ely In C, why can I pass a character array to a function that takes a char *as a parameter, but not the address of the array to a function that takes a char **? UPDATE: Interestingly, changing the parameter type char* qux[12]to does n't completely change

C - pass pointer to function

Bata Can someone tell me why this code doesn't work? I tried to compile and run it with gcc 6.3, but the result of printf is a memory mess: "P@" instead of "something". Compiling it with gcc 5.3 at https://www.jdoodle.com/c-online-compiler gave me the exact re

Another way to pass a char pointer to a function

sebnaq Given that the source code comes fromstrcpy() char * strcpy(char *s1, const char *s2) { char *s = s1; while ((*s++ = *s2++) != 0); return (s1); } Why isn't the second parameter handed over and how does it look in memory since I'm not passin

Pass a string to a function that accepts a char pointer

Libru I've been using the OpenSSL library in C for a long time, but now I need to migrate to C++. The documentation for OpenSSL describes the MD5 feature like this. unsigned char *MD5(const unsigned char *d, unsigned long n, unsigned char *md);

Pass a string to a function that accepts a char pointer

Libru I've been using the OpenSSL library in C for a long time, but now I need to migrate to C++. The documentation for OpenSSL describes the MD5 feature like this. unsigned char *MD5(const unsigned char *d, unsigned long n, unsigned char *md);

C++ pass function pointer

Sahar Rabinoviz I have the following function static void p (){ } I want to pass a function pointer to p to function x. void x(void * ptr){ } I am trying the following but it doesn't work. ... x(ptr); Note that x and p are in different classes. I am getti