fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int test(char **output, int *output_len) {
  6. int old_output_len;
  7.  
  8. printf("\ninner before: %d\n", *output);
  9.  
  10. // Add text first time
  11. old_output_len = *output_len;
  12. *output_len += 11;
  13. printf("%d\t", *output_len);
  14.  
  15. *output = realloc(*output, *output_len);
  16. memset(*output + old_output_len, 0, 11);
  17. *output = strcat(*output, "this is 11");
  18. printf("%d\n", *output);
  19.  
  20. /*
  21.   // Extend text
  22.   old_output_len = *output_len;
  23.   *output_len += 11;
  24.   printf("%d\t", *output_len * sizeof(char));
  25.  
  26.   // Ignore, now this doesn't get new pointer, unlike in previous code! but it still works though
  27.   &output = realloc(output, *output_len * sizeof(char));
  28.   memset(output + old_output_len, 0, 11);
  29.   *output = strcat(&output, "this is 11");
  30.   printf("%d\n", output);
  31.   printf("\ninner after: %d\n", output);*/
  32. }
  33.  
  34. int main() {
  35. char *output = NULL;
  36. int output_len = 0;
  37.  
  38. printf("\nbefore: %d\n", &output);
  39. test(&output, &output_len);
  40. printf("\nafter:%d\n", &output);
  41.  
  42. // This prints 0, wasn't output pointer updated by test?
  43. printf("\n\n%d\n\n", output);
  44. printf("%d\n", output_len);
  45.  
  46. free(output);
  47. //system("pause");
  48.  
  49. return EXIT_SUCCESS;
  50. }
  51.  
Success #stdin #stdout 0s 2380KB
stdin
Standard input is empty
stdout
before: -1073937240

inner before: 0
11	151134216

after:-1073937240


151134216

11