0

I have an integer array:

int a[4] = {192,168,8,100};

I need to store these numbers in a char array like follows:

char ip[15] = "192.168.8.100";

Is there any library functions in C to use?

4
  • 6
    sprintf(ip,"%d.%d.%d.%d",a[0],a[1],a[2],a[3]) should suffice. Commented May 6, 2017 at 20:35
  • 4
    Also char ip[15] --> char ip[16] Commented May 6, 2017 at 20:35
  • Thanks. It's simple. :) Commented May 6, 2017 at 20:41
  • Probably not strictly necessary here, but you should use snprintf() rather than sprintf() to prevent any possibility of a buffer overrun. Always bounds-check in C. Code defensively! Commented May 7, 2017 at 1:21

2 Answers 2

1

If you want that to be on the heap and dynamically allocate memory you can use the asprintf.

char *ip = NULL;
int ret;
ret = asprintf(&ip, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
if (ret < 0)
{  
 // error handling
}
// use ip
free(ip); 
ip = NULL;

Otherwise, if you know the size before hand you can use sprintf.

sprintf(ip, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);

Personally, I prefer to use asprintf, however, you need to remember to free that memory allocated by asprintf otherwise you will get a memory leak.

Side note, asprintf calls realloc internally so you could use it for string concatenation as long as you free successive calls.

Sign up to request clarification or add additional context in comments.

2 Comments

asprintf is not standard.
Line 3 in your first block should instead be ret = asprintf(&ip, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
0

You always, always, always check that you aren’t overrunning your buffer in C, and especially when you’re dealing with data you got over a network.

For example, you set the array to fifteen characters, but the string "100.100.100.100" has sixteen with its terminating null. That works for now, but is precariously brittle. Or you might get buggy or malicious input like {-1000000000L, -1000000000L, -1000000000L, -1000000000}. Or you might later refactor to support IPv6.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define DOTTED_IPV4_LEN     16U

int main(void)
{
  static const unsigned char a[4] = {192,168,8,100};
  char ip[DOTTED_IPV4_LEN];

  snprintf( ip, DOTTED_IPV4_LEN, "%u.%u.%u.%u", a[0], a[1], a[2], a[3] );
  puts(ip);

  return EXIT_SUCCESS;
}

In fact, some shops, including I think Microsoft, wouldn’t even accept the destination buffer being declared on the stack like that and would demand the extra precaution of making ip a pointer to dynamically-allocated memory, perhaps:

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define DOTTED_IPV4_LEN     16U

int main(void)
{
  static const unsigned char a[4] = {192,168,8,100};
  char (* const ip)[DOTTED_IPV4_LEN] = malloc(sizeof(*ip));

  assert(ip);
  snprintf( *ip, sizeof(*ip), "%u.%u.%u.%u", a[0], a[1], a[2], a[3] );
  puts(*ip);

  free(ip); // Unnecessary because we're about to quit.
  return EXIT_SUCCESS;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.