I've been instructed to created a model strdup
char* modelstrdup(char* source);
-- This function is a mirror of the standard C library function called strdup. The parameter is a string that we wish to duplicate. The returned pointer will point onto the heap. When you write this function, create a String struct on the heap that holds a copy of source. Set the length and capacity of your String equal to the number of characters in source. Be sure to return the address to the first character in your String (and not the address of the String struct itself).
This is the only hint my professor gave me, but I don't even know where to start...
// Example only, client programmers won‟t do this!
char* s; // should be “yo!” when we‟re done
String* new_string = malloc(sizeof(String) + 10 + 1);
(*new_string).length = 3; // 3 characters in “yo!”
(*new_string).capacity = 10; // malloc‟d 10 bytes
(*new_string).signature = ~0xdeadbeef;
(*new_string).ptr[0] = „y‟;
(*new_string).ptr[1] = „o‟;
(*new_string).ptr[2] = „!‟;
(*new_string).ptr[3] = 0;
s = (*new_string).ptr;
printf(“the string is %s\n”, s);
Will someone please help me get started? Thank you!