Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- char* makeCopy(char* c)
- {
- int len = 0, i;
- char* p = c;
- char* copy;
- while(*p) {
- len++;
- p++;
- }
- // Don't forget to dynamically allocate memory for copy. I almost always forget it :(
- copy = (char*) malloc(sizeof(char) * (len + 1)); // len + 1, not len because we need space for null character too.
- for (i = 0; i <= len; i++) // I have written i <= len, not i < len, because I want to copy the null character too.
- *(copy + i) = *(c + i);
- return copy;
- }
- int main()
- {
- char message[1000] = "Hi there";
- char *copy = makeCopy(message);
- printf("original : %s\taddress: %p\n", message, message);
- printf("copy : %s\taddress: %p\n", copy, copy);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement