| string-copy-strange.c - En variant af string_copy der opbygger kopien i lokalt lager. | Lektion 7 - slide 21 : 26 Program 2 |
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* Copy s to a locally allocated string and return it */
char *string_copy(const char *s){
static char new_str[8]; /* static is essential. Zero initialized */
strncpy(new_str,s,7);
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;
}