1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #include <stdio.h> #include <stdlib.h> void concat(char *p, char *str1, char *str2) { int i,j; for (i = 0; str1[i]; i++) { p[i] = str1[i]; } for (j = 0; str2[j]; j++) { p[i++] = str2[j]; } p[i] = 0; } int main(void) { char str1[10] = "hello", str2[10] = "world!!!", *p; p = (char *)malloc(sizeof(char) * 20); if (p == (char *)NULL) { fprintf(stderr, "malloc fail!!!\n"); exit(1); } concat(p, str1, str2); printf("%s \n", p); } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <stdio.h> #include <stdlib.h> char strings[2][10] = { "Hello", "World" }; void set_pointer(char **p, int n) { *p = strings[n]; } int main(void) { char *p; set_pointer(&p, 0); printf("%s \n", p); set_pointer(&p, 1); printf("%s \n", p); } | cs |