6

Is it at all possible to use snprintf to print an array? I know that it can take multiple arguments, and it expects at least as many as your formatting string suggests, but if I just give it 1 formatting string and an array of values, will it print the entire array to the buffer?

The reason I ask, is because I am modifying source code, and the current implementation only supported one value being placed in a string, but I am modifying it to support an array of values. I want to change the original implementation as little as possible.

If this doesn't work, is there another way someone would recommend to do this? Should I just suck it up and use a for loop (how well would that really work without stringbuffers)?

Essentially: What would be the best way to get all of the values from an array of doubles into the same string for a return?

Nealon
  • 2,213
  • 6
  • 26
  • 40
  • possible duplicate of [Combine two constant strings (or arrays) into one constant string (or array) at compile time](http://stackoverflow.com/questions/3154170/combine-two-constant-strings-or-arrays-into-one-constant-string-or-array-at) – hd1 Jun 05 '13 at 13:57
  • 1
    @hd1: I don't think it is related to that question. – undur_gongor Jun 05 '13 at 19:06
  • @undur_gongor neither do I – Nealon Jun 06 '13 at 13:43

2 Answers2

5

No, there's no formatting specifier for that.

Sure, use a loop. You can use snprintf() to print each double after the one before it, so you never need to copy the strings around:

double a[] = { 1, 2, 3 };
char outbuf[128], *put = outbuf;

for(int = 0; i < sizeof a / sizeof *a; ++i)
{
  put += snprintf(put, sizeof outbuf - (put - outbuf), "%f ", a[i]);
}

The above is untested, but you get the general idea. It separates each number with a single space, and also emits a trailing space which might be annoying.

It does not do a lot to protect itself against buffer overflow, generally for code like this you can know the range of the inputs and make sure the outbuf is big enough. For production code you would need to think about this of course, the point here is to show how to solve the core problem.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • will it automatically append the buffer and not overwrite it? – Nealon Jun 05 '13 at 13:57
  • @Nealon The above will append, yes. Notice how `ptr` is used to point at the next location in the string buffer where a number needs to go, and incremented as we go along. It handles different lengths of stringized doubles by using `snprintf()`'s return value. – unwind Jun 05 '13 at 13:59
  • `sizeof outbuf - (put - outbuf)` -- why is this necessary? wouldn't `sizeof ptr` suffice? – Nealon Jun 05 '13 at 14:05
  • @Nealon It computes the number of remaining characters. No, `sizeof ptr` is all wrong, it's just "size of the pointer `ptr`", which is constant through the loop and has nothing to do with the size of the memory block `ptr` points into. – unwind Jun 05 '13 at 14:11
  • The above code does not protect from buffer overflows. The second argument of snprintf is an unsigned (size_t) which means that if this argument is negative as a signed it will be cast as a BIG unsigned number and potentially create a buffer overflow. Note that snprintf does not return the number of bytes really written but return the number of bytes which should have been written if enough space had been available. – leszek.hanusz Dec 14 '16 at 17:51
0

I decided to go with this:

int ptr = 0;
for( i = 0; i < size; i++)
{
    ptr += snprintf(outbuf + ptr, sizeof(outbuf) - ptr, "%.15f ", values[i]);   
}

slightly different, but to the same effect as in @unwind 's solution. I got this idea from the reference page for snprintf()

Nealon
  • 2,213
  • 6
  • 26
  • 40
  • 2
    The above code does not protect from buffer overflows. The second argument of snprintf is an unsigned (size_t) which means that if this argument is negative as a signed it will be cast as a BIG unsigned number and potentially create a buffer overflow. Note that snprintf does not return the number of bytes really written but return the number of bytes which should have been written if enough space had been available. – leszek.hanusz Dec 14 '16 at 17:51