| string-copy.c - Hele programmet. | Lektion 7 - slide 21 : 26 Program 1 |
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* Copy s to a fresh allocated string and return it */
char *string_copy(const char *s){
static char *new_str;
new_str = (char *)malloc(strlen(s)+1);
strcpy(new_str,s);
return new_str;
}
int main(void) {
char s[] = "Aalborg University", *t;
t = string_copy(s);
strcpy(s,"---"); /* destroy s */
printf("The original is: %s.\nThe copy is: %s\n", s, t);
return 0;
}