fork(3) download
  1. #include<stdio.h>
  2. #include<string.h>
  3. static char* abc = "kj";
  4.  
  5. // What exactly does this function do?
  6. void fn(char**s) {
  7. printf("arg: %p\n", s);
  8. printf("arg deref before: %p\n", *s);
  9.  
  10. // This throws. *s is not a valid address.
  11. // printf("contents: ->%s<-\n", *s);
  12.  
  13. *s = abc;
  14. printf("arg deref after assigning abc: %p\n", *s);
  15.  
  16. // Now *s holds a valid address (that of "kj").
  17. printf("contents: ->%s<-\n", *s);
  18.  
  19. }
  20.  
  21. // Helper function to print a char pointer and the first bytes
  22. // it points to
  23. void printStr(const char *const caption, const char *const ptr)
  24. {
  25. int i=0;
  26. printf("%s: ->%s<-, i.e. {", caption, ptr);
  27. for( i=0; i<sizeof(char *)-1; ++i)
  28. {
  29. printf("0x%x,", ptr[i]);
  30. }
  31. printf( "0x%x ...}\n", ptr[sizeof(char *)-1] );
  32. }
  33.  
  34. int main() {
  35. char str[256];
  36.  
  37. printf("size of ptr: %zu\n", sizeof(void *));
  38. strcpy(str, "AAAAAAAA"); // 9 defined bytes
  39. printStr("str", str);
  40. printf("arr addr: %p\n", &str);
  41. printf("addr of abc: %p\n", abc);
  42.  
  43. fn(&str);
  44.  
  45.  
  46. printStr("str after fn (a pointer value, only accidentally printable): ", str);
  47. printf("arr addr after fn: %p\n", &str);
  48.  
  49. return 0;
  50. }
Success #stdin #stdout 0s 2296KB
stdin
Standard input is empty
stdout
size of ptr: 4
str: ->AAAAAAAA<-, i.e. {0x41,0x41,0x41,0x41 ...}
arr addr: 0xbfa9bc40
addr of abc: 0x804868f
arg: 0xbfa9bc40
arg deref before: 0x41414141
arg deref after assigning abc: 0x804868f
contents: ->kj<-
str after fn (a pointer value, only accidentally printable): : ->��AAAA<-, i.e. {0xffffff8f,0xffffff86,0x4,0x8 ...}
arr addr after fn: 0xbfa9bc40