1

For example:

char* function(int n, ...); //prototype
main()
{
 char* s;
 s= function(3, 123, 456, 789);
 printf("%s", s);
}

How do i write this function that will take integers and return a dynamic string of these integers? While trying to solve this, I have came across function itoa(); but I couldn't find enough things about it and there were very few examples using it, and also, should I dynamically allocate a character string in C and since (at least I think this is how it goes) I will be putting those integers in order one by one, should I use realloc for that string because its size will get bigger and bigger until im done? Do I use strcat to make basically a string of strings? And probably my biggest question, when and how in all that mess do I use function itoa()? Function should return

"123456789"

EDIT: Thank you all for help, I took into consideration everything said and managed to solve this problem. Here is my final code (probably could have done a better job about memory management):

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

char * function(int n, ...);

int main()
{
    char* s;
  s = function(3, 123, 456, 789);
  printf("%s", s);
  free(s);
  s=function(9, 123,456,789,123,789, 12, 433, 553, 341);
  printf("\n%s", s);
  return 0;
}
char * function(int n, ...)
{
    char str[100]={0};
    char* res=0;
    int i, br;
  va_list args;
  va_start(args, n);

  for (i = 0; i < n; i++)
  {
      br=va_arg(args, int);
      itoa(br, str, 10);
      char* tmp=(char *)malloc(sizeof(str));
      strcpy(tmp, res ? res : "");
      strcat(tmp, str);
      free(res);
      res=tmp;
  }
  va_end(args);
  return res;
}
savara7
  • 33
  • 4
  • 4
    For printing numbers into a string, you can take a look at `sprintf`. And yes, you need to allocate the array for the string dynamically. You should first show us your attempt to implement the function. Then you can see specifically which part of it does not work and ask more detailled question then. – Gerhardh Mar 18 '22 at 16:58
  • I think you will need `1 + \sum_i ceil(log_10 num_i)` (assuming natural numbers) space to fit the result. `snprintf` with `n` as 0 will give you the space. – Neil Mar 18 '22 at 17:12
  • 1
    The first step is to write the code that handles the variable arguments. It should just retrieve each argument and print it to the screen. Once that works, you can think about the next step. – user3386109 Mar 18 '22 at 17:15
  • @Neil What if numbers are (10, 10, 10, 10, 10 ... 10)? – जलजनक Mar 18 '22 at 17:16
  • `itoa` is not part of the C standard library. It's a function that appeared in K&R as an example of how to use a `do-while` loop. That's why you won't find much information about it. You should be using `snprintf` to convert the numbers to strings. If you pass `NULL` and `0` as the output pointer and output length, `snprintf` will return the character count needed to hold the string. Do that for all the numbers *before* allocating memory, and you'll be able to do one allocation that's correct for the final string. – user3386109 Mar 18 '22 at 17:23
  • 3
    Don't use realloc, just don't be stingy. There is an upper bound on the length of each value as a string (depending on INT_MAX). Multiply that by n and allocate once. – William Pursell Mar 18 '22 at 17:29
  • Sorry, you need `vsnprintf`, but the format string is replaced by a number, so you might as well do it yourself. Either have a maximum, or a dynamic array, or (probably the simplest) calculate it first, do the allocation, and store the number again. Lots of dividing by 10. – Neil Mar 18 '22 at 17:29

1 Answers1

2

C Algorithm

  1. Determine worse cast size buffer for one int. Hint: As text, how long is INT_MIN?

  2. Allocate a buffer that is n*worst_case + 1.

  3. Set buf[0] = '\0';, size_t length_used = 0;

  4. Walk the variadic arguments with va_start(), va_arg(), va_end() and friends. One at a time, convert using sprintf() into the end of the used buffer. Use the return value of the sprintf() to keep track of how far the used portion of the buffer has grown.

  5. Perform a final realloc() to shrink and right-size the allocated buffer if desired.

  6. Code code checks for errors in allocation and printing.

  7. Take a walk and enjoy the week-end as spring is coming. (or maybe autumn?)


strcat() not needed.

itoa() not needed.


Advanced

To determine the bit width of value bits in an int (even if it has padding) use IMAX_BITS(INT_MAX).

To find the number of decimal digits given the bit width:

// bit_width * log2(10) rounded up
IMAX_BITS(m)*28/93 + 1

Account for the '-'.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256