SHARE
TWEET

Untitled

a guest Aug 24th, 2017 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. char* ptr;
  2. strcpy(ptr, "hello world"); // crash here!
  3.    
  4. char* ptr;
  5. scanf("%s", ptr); // crash here!
  6.    
  7. {
  8.   int data = 0;
  9.   int* ptr = &data;
  10.   ...
  11. }
  12.    
  13. int* ptr = malloc(sizeof(int));
  14.    
  15. /*** examples of incorrect use of pointers ***/
  16.  
  17. // 1.
  18. int* bad;
  19. *bad = 42;
  20.  
  21. // 2.
  22. char* bad;
  23. strcpy(bad, "hello");
  24.    
  25. /*** examples of correct use of pointers ***/
  26.  
  27. // 1.
  28. int var;
  29. int* good = &var;
  30. *good = 42;
  31.  
  32. // 2.
  33. char* good = malloc(5+1); // allocates memory for 5 characters and 1 terminator
  34. strcpy(good, "hello");
  35.    
  36. int* p1 = NULL; // pointer to nowhere
  37. int* p2;        // uninitialized pointer, pointer to "anywhere", cannot be used yet
  38.    
  39. char * strcpy ( char * destination, const char * source );
  40.    
  41. char* ptr = malloc(32);  
  42. strcpy(ptr, "hello world");
  43.    
  44. char str[32];  
  45. strcpy(str, "hello world");
  46.    
  47. char *ptr = malloc(32);
  48. scanf("%31[^n]", ptr);
RAW Paste Data
Top