Advertisement
yorath

Pass dynamic memory

Nov 4th, 2012
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.43 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <string.h>
  3.  
  4. /*
  5. void GetMemory(char *p, int num)
  6. {
  7.     p = (char *)malloc(sizeof(char) * num);
  8. }
  9.  
  10. int main()
  11. {
  12.     char *str = NULL;
  13.     GetMemory(str, 100);
  14.     // str is still NULL
  15.     strcpy(str, "hello");
  16.     return 0;
  17. }
  18. */
  19.  
  20. void GetMemory(char **p, int num)
  21. {
  22.     *p = (char *)malloc(sizeof(char) * num);
  23. }
  24.  
  25. int main()
  26. {
  27.     char *str = NULL;
  28.     GetMemory(&str, 100);
  29.     strcpy(str, "hello");
  30.     return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement