tags:

views:

68

answers:

1

i want to get ipaddress of mycomputer in a variable this statement prints ipaddress of my computer but i want to assign it in a varible how can i fetch the value of ipaddress from inet_ntoa(inaddrr(ifr_addr.sa_data)) into an variable ip of char* type .

printf("IP Address: %s\n", inet_ntoa(inaddrr(ifr_addr.sa_data)));
+1  A: 

inet_ntoa already returns a char * - it's just that it points to a static buffer, which will be overwritten on subsequent calls. If you want to save that pointed-to string, you can just use strdup():

char *ip;
/* ... */
ip = strdup(inet_ntoa(inaddrr(ifr_addr.sa_data)));

You should call free() on ip when you are done with it.

caf