Reputation: 65
struct in_addr a,b,c,d;
if(inet_aton ("10.0.0.1", &a)!=-1 );
printf("a:%s\n", inet_ntoa(a));
if(inet_aton ("10.0.0.2", &b)!=-1 )
printf("b:%s\n", inet_ntoa(b));
It's no problem to print the to IP address:
a:10.0.0.1
b:10.0.0.2
However I found that:
inet_aton ("10.0.0.3", &c);
inet_aton ("10.0.0.4", &d);
printf("c:%s %s\n", inet_ntoa(c), inet_ntoa(d));
printf("d:%s\n", inet_ntoa(d));
It prints:
c:10.0.0.3 d:10.0.0.3
d:10.0.0.4
The strange things is that it prints the wrong IP of d
at this line:
printf("c:%s %s\n", inet_ntoa(c), inet_ntoa(d));
I don't know why!
Upvotes: 4
Views: 1769
Reputation: 22562
The manual for inet_ntoa
says:
The string is returned in a statically allocated buffer, which subsequent calls will overwrite.
You have two functions in printf("c:%s %s\n", inet_ntoa(c), inet_ntoa(d));
that overwrite the same buffer.
Try if (inet_ntoa(c) == inet_ntoa(d))
, you may be surprised by the result.
Upvotes: 7