Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi guys! I'm trying to write a program that uses raw sockets, but when I try to compile this line:
C++
printf("Indirizzo origine: %s\n", inet_ntoa(ip->saddr));

I get this error:
warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
        printf("Indirizzo origine: %s\n", inet_ntoa(ip->saddr));

"ip" is declared as follows:
C++
struct iphdr *ip;

Isn't inet_ntoa() function returning a string? What am I doing wrong?
Posted

I solved it by using the inet_ntop() function instead of the first I was using.
 
Share this answer
 
Yes, by documentation it's supposed to return char*. Check up what is that. Maybe you included wrong header, and a different function, but it's more likely that, by some weird reason, you use this function call before the point where the function is declared. This shame could take place only because you use C, not C++.

Stackoverflow expert suggests:

The incorrect return-type for inet_ntoa is the result of an old C rule that states that if you try to use a function without a prior declaration, then the compiler must assume the function returns an int and takes an unknown (but fixed) number of arguments. The mismatch between the assumed return type and the actual return type of the function results in undefined behaviour, which manifests itself as a crash in your case.

The solution is to include the correct header for inet_ntoa.
I would suggest more: use C++ instead, it would guard you at least from some of totally unnecessary troubles.

—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900