fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. char charheap[32];
  7.  
  8. char *char_alloc(int s) {
  9. int len = 32;
  10. for (int i = 0; i < len; i++) {
  11. if (charheap[0] == '\0') {
  12. //char a = charheap[0];
  13. return charheap;
  14. } else if (charheap[i] == '\0') {
  15. //char b = charheap[i+1];
  16. return &charheap[i+1];
  17. }
  18. }
  19. return NULL;
  20. }
  21.  
  22. void arr_print () {
  23. for (char *p = charheap; p < charheap + sizeof(charheap); ++p) {
  24. if (isprint(*p)) putchar(*p);
  25. else putchar('\\');
  26. }
  27. putchar('\n');
  28. }
  29.  
  30. char *alloc_and_print(int s, const char *cpy) {
  31. char *ncb = char_alloc(s);// allocate the next contiguous block
  32. if (ncb == NULL) {
  33. printf("Failed\n");
  34. } else {
  35. strcpy(ncb, cpy);
  36. arr_print();// print the array
  37. }
  38. return ncb;
  39. }
  40.  
  41. int main(void) {
  42. char *str1 = alloc_and_print(5, "hello");
  43. char *str2 = alloc_and_print(5, "brian");
  44. puts(str1);
  45. puts(str2);
  46. }
  47.  
Success #stdin #stdout 0s 1832KB
stdin
Standard input is empty
stdout
hello\\\\\\\\\\\\\\\\\\\\\\\\\\\
hello\brian\\\\\\\\\\\\\\\\\\\\\
hello
brian