Journal ryanr's Journal: Bug Puzzle #2 4
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
struct
{
struct in_addr src;
struct in_addr dest;
} addrs;
int main()
{
inet_aton ("10.0.0.1", &addrs.src);
inet_aton ("172.16.0.1", &addrs.dest);
printf ("src: %s, dest: %s\n", inet_ntoa(addrs.src), inet_ntoa(addrs.dest));
}
[root@scutter root]# gcc bug.c
[root@scutter root]# ./a.out
src: 10.0.0.1, dest: 10.0.0.1
wtf?
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
struct
{
struct in_addr src;
struct in_addr dest;
} addrs;
int main()
{
inet_aton ("10.0.0.1", &addrs.src);
inet_aton ("172.16.0.1", &addrs.dest);
printf ("src: %s, dest: %s\n", inet_ntoa(addrs.src), inet_ntoa(addrs.dest));
}
[root@scutter root]# gcc bug.c
[root@scutter root]#
src: 10.0.0.1, dest: 10.0.0.1
wtf?
Re: (Score:1)
Re:Well, you got me... (Score:2)
The inet_ntoa() function converts the Internet host address in given in network byte order to a string in standard numbers-and-dots notation. The string is returned in a statically allocated buffer, which subsequent calls will overwrite.
Re: (Score:1)
Re:Well, you got me... (Score:2)
I assume the reason the functon works this way is to avoid allocating storage each time (which the programmer would have to remember to free() )