fork(2) download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. void get_me_a_string(int * int_array, int array_size, char * output_string, int output_string_max_size)
  6. {
  7. if(!int_array || !output_string)
  8. return;
  9.  
  10. char * aux_string = NULL;
  11.  
  12. //Depending on the compiler int is 2-byte or 4 byte.
  13. //Meaning INT_MAX will be at most 2147483647 (10 characters + 1 '\0').
  14. aux_string = (char *) malloc(11);
  15. if(!aux_string)
  16. return;
  17.  
  18. int i;
  19. int current_array_size = 0;
  20. for(i = 0; i < array_size; i++)
  21. {
  22. sprintf(aux_string, "%d", int_array[i]);
  23. current_array_size += strlen(aux_string);
  24. if(current_array_size < output_string_max_size)
  25. strcat(output_string, aux_string);
  26. else
  27. break;
  28. }
  29.  
  30. free(aux_string);
  31. }
  32.  
  33. int main(void) {
  34. int a[5]={5,21,456,1,3};
  35.  
  36. int string_max_size = 256;
  37. char * string_from_array = NULL;
  38.  
  39. string_from_array = (char *) malloc(string_max_size);
  40.  
  41. if(NULL == string_from_array)
  42. {
  43. printf("Memory allocation failed. Exiting...");
  44. return 1;
  45. }
  46.  
  47. memset(string_from_array, 0, string_max_size);
  48. get_me_a_string(a, 5, string_from_array, string_max_size);
  49.  
  50. printf(string_from_array);
  51.  
  52. free(string_from_array);
  53. return 0;
  54. }
  55.  
  56.  
Success #stdin #stdout 0s 2184KB
stdin
Standard input is empty
stdout
52145613