fork download
  1. /* Write a function, char* strdup(const char*), that copies a C-style string into memory it
  2. allocates on the free store. Do not use any standard library functions. Do not use subscripting;
  3. use the dereference operator * instead. */
  4.  
  5.  
  6. #include <iostream>
  7. #include <string.h>
  8.  
  9.  
  10. char* strdup(const char* s) {
  11.  
  12. int size = {sizeof(s)/sizeof(char)};
  13.  
  14. char* str_ptr = new char[size];
  15. strcpy(str_ptr, s);
  16.  
  17. return str_ptr;
  18. }
  19.  
  20. int main()
  21. {
  22. char* rdy = strdup("ass");
  23. std::cout << rdy << std::endl;
  24. }
Success #stdin #stdout 0s 4388KB
stdin
Standard input is empty
stdout
ass