Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sprintf and unsigned int array in C

I have a pointer to an array of ints and the length of the array as such:

unsigned int length = 3;
int *array;        // Assume the array has 3 initialized elements

I also have a string and a buffer (assume it is sufficiently large) to put into sprintf as such:

char buffer[128];
const char *pattern = "(%d, %d, %d)\n";

Assume that pattern will only have "%d"s and other characters in it, but could be any form (i.e. "Test %d: %d" or "%d %d"), and that the length of array will always be the same as the number of "%d"s.

Since the length of the array can be anything, is there any way that I can do sprintf (buffer, pattern, &array[0], &array[1], &array[2]) without explicitly enumerating the elements of array? Something along the lines of sprintf (buffer, pattern, array). I can write as many helper functions as are necessary. I was thinking of faking a va_list, but this seems to be bad practice as it restricts the program to a certain compiler.

like image 997
Chirag Avatar asked Dec 13 '12 01:12

Chirag


People also ask

What is unsigned integer array?

The unsigned int can contain storage size either 2 or 4 bytes where values ranging from [0 to 65,535] or [0 to 4,294,967,295]. The format specifier used for an unsigned int data type in C is “ %u ”.

What is use of sprintf in C?

sprintf stands for "string print". In C programming language, it is a file handling function that is used to send formatted output to the string. Instead of printing on console, sprintf() function stores the output on char buffer that is specified in sprintf.

Where is sprintf defined in C?

The function is declared in C as: int sprintf(char *str, const char *format, [arg1, arg2, ... ]); where, str is a character array on which data is written. format is a C string which describes the output, along with placeholders for the integer arguments to be inserted in the formatted string.

What does sprintf mean?

sprintf stands for “String print”. Instead of printing on console, it store output on char buffer which are specified in sprintf.


3 Answers

Passing all elements in a single va_list is not going to help, because the format string needs to be created in a loop anyway. Since you cannot escape the loop anyway, you might as well do the printing in the same loop:

int data[] = {12, 345, 6789, 101112};
char buf[128], *pos = buf;
for (int i = 0 ; i != 4 ; i++) {
    if (i) {
        pos += sprintf(pos, ", ");
    }
    pos += sprintf(pos, "%d", data[i]);
}
printf("%s\n", buf);

Here is a link to a demo on ideone.

like image 79
Sergey Kalinichenko Avatar answered Sep 23 '22 21:09

Sergey Kalinichenko


you can do something like...

char* format_uint_array(char *b, unsigned int* data, int length, char* delim, char* fmt)
    {
        int i;
        char s[50];
        b[0] = 0;
        for( i = 0; i < length; i++ )
        {
            s[0]=0;
            sprintf( s, fmt, data[i], (i<length-1)?delim : "");
            strcat(b, s);    
        }
        return b;
    }

then use it like

char buffer[128];
char formattedints[128];
sprintf("(%s)\n", format_uint_array(formattedints, array, 3, ", ", "%d%s"));
like image 24
Keith Nicholas Avatar answered Sep 25 '22 21:09

Keith Nicholas


No loops:

#include <stdio.h>

int array[3] = {1, 2, 3};     
char buffer[128];

char *array_to_str(char * str, int *array, unsigned int n) {
  int r;
  if (n == 0) return 0;
  if (n == 1) r = sprintf(str, "%d", array[0]);
  else        r = sprintf(str, "%d, ", array[0]);
  array_to_str(str + r, array + 1, n - 1); 
  return str;
}

int main() { 
  printf("(%s)\n", array_to_str(buffer, array, 3));
  return 0;  
} 
like image 40
perreal Avatar answered Sep 22 '22 21:09

perreal