0

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)));
Anders Abel
  • 67,989
  • 17
  • 150
  • 217
neha soni
  • 9
  • 1

1 Answers1

2

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
  • 233,326
  • 40
  • 323
  • 462