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: 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);
}
-
Answer # 1
Related articles
- i want to compile a pcap file
- c - i want to find the standard deviation of 10 real data
- python - i want to plot dict type data by color for each key
- c - i want to make a 10x10 board
- i want to judge the value from a1 to c3 or 1a to 3c
- i want to complete this program
- i want to find the fade position with opencv
- i want to know the xcode type declaration
- i want to find the sum of a sequence in c language
- i just want to practice the code
- c - i want to get a texture somehow
- i want to read the data of another file in c language
- c - i want to set the 10's and 1's to 0 for items with a tax of 10,000 yen or more
- change int type to char type
- i want to write code for everyone
- [ruby] i want to fix int type at the time
- i want to create and call a function
- c - about the meaning of ((void (*)())buf)();
- c - i want to make a game of winning when you have all 5 on a 15 x 15 board
- python 3x - i want to append dataframe type data to a list with dict type
Trends
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:
The same can be written as:
Addition
Question code
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
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.