Home>

I tried to code while checking on the net, but I want to cast the void type to uint32_t type, but it is an error and I am in trouble.
Eventually, I will make a Linux command inet_pton.
I would appreciate your help.
Thank you.

Error message
Error: Invalid conversion from ‘uint32_t {aka unsigned int}’ to ‘const void *’ [-fpermissive]
     memcpy (dst, No.number1, 4);
Error: initializing argument 2 of ‘void * memcpy (void *, const void *, size_t)’ [-fpermissive]
 extern void * memcpy (void * __ restrict __dest, const void * __ restrict __src,
Applicable source code
#include 
#include 
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int inet_pton (const char * src, void * dst) {
    // variable declaration
    int suuti1, suuti2, suuti3, suuti4;
    // union type definition
    union number {
        uint32_t number1;
        uint8_t number2 [4];
    };
    // Declaring union variable names
    union number No;
    // convert src string to number
    sscanf (src, "% d.% d.% d.% d",&suuti1,&suuti2,&suuti3,&suuti4);
    // Store the value converted to numeric value in a variable
    No.number2 [0] = suuti1;
    No.number2 [1] = suuti2;
    No.number2 [2] = suuti3;
    No.number2 [3] = suuti4;
    // cast void type dst to uint32_t type
    No.number1 = (uintptr_t)&dst;
    // copy 4 bytes memory of number1 to dst
    memcpy (dst, No.number1, 4);

    return (1);
}
int main () {
    int result;
    struct in_addr in_addr;
    result = inet_pton ("130.0.7.23",&in_addr);
    assert (result == 1&&in_addr.s_addr == ((23<<24) | (7<<16) | (0<<8) | 130));
    result = inet_pton ("130.00.7.23",&in_addr);
    assert (result == 0);
    result = inet_pton ("130.01.7.23",&in_addr);
    assert (result == 0);
    return (0);
}
c
  • Answer # 1

    void type cannot be converted to uint32_t type.
    inet_pton is a function, not a command.
    since inet_pton is declared in
    The same function cannot be defined in C.
    Let's call it my_inet_pton.

    You can write:

    # include 
    #include 
    #include
    #include
    #include
    int my_inet_pton (const char * src, void * dst)
    {
        int suuti1, suuti2, suuti3, suuti4;// variable declaration
        union number {// union type definition
            uint32_t number1;
            uint8_t number2 [4];
        };
        union number No;// Declaring union variable name
        // convert src string to number
        if (sscanf (src, "% d.% d.% d.% d",&suuti1,&suuti2,&suuti3,&suuti4)! = 4)
            return 0;
        // Store the value converted to numeric value in a variable
        No.number2 [0] = suuti1;
        No.number2 [1] = suuti2;
        No.number2 [2] = suuti3;
        No.number2 [3] = suuti4;
        memcpy (dst,&No.number1, 4);
        return 1;
    }
    int main (void)
    {
        unsigned char buf [4];
        int n = my_inet_pton ("216.58.197.174", buf);
        printf ("% d.% d.% d.% d \ n", buf [0], buf [1], buf [2], buf [3]);
    }


    The same can be written as:

    # include 
    #include 
    #include
    int my_inet_pton (const char * src, void * dst)
    {
        char * p = (char *) dst;
        if (sscanf (src, "% hhd.% hhd.% hhd.% hhd", p, p + 1, p + 2, p + 3) == 4)
            return 1;
        return 0;
    }
    int main (void)
    {
        unsigned char buf [4];
        int n = my_inet_pton ("216.58.197.174", buf);
        printf ("% d.% d.% d.% d \ n", buf [0], buf [1], buf [2], buf [3]);
    }

    Addition
    Question code

    // cast void type dst to uint32_t type
        No.number1 = (uintptr_t)&dst;


    The comment is incorrect.
    The type of dst is not a void type, but a void *, that is, a "pointer to void" type.
    &dst is the address of the argument dst.
    &dst is of type void * *, that is, a "pointer to pointer to void" type.
    (uintptr_t) casts void * * to unsigned int *
    The type of No.number1 is uint32_t, that is, unsigned int.
    In a compiler with a 4-byte pointer, No.number1 will contain the value of that pointer.
    In a compiler with a pointer of 8 bytes, No.number1 will contain the lower 32 bits of the pointer value.
    Do you know the meaning of union?
    No has only 4 bytes.
    It contains the values ​​assigned to No.number2 [0] to No.number2 [3].
    Assigning to No.number1 destroys the value assigned to .number2.
    This is because the address of the argument dst is assigned to No.number1.

    Question code

    // Copy the memory of 4 bytes of number1 to dst
        memcpy (dst, No.number1, 4);


    The second argument of memcpy must be passed the source address.
    Therefore, memcpy (dst,&No.number1, 4);must be done.
    No.number1 = (uintptr_t)&dst; No.number1 already contains the value assigned to .number2, so
    Memcpy can achieve your goal.