fivePointFive() { /* s is an array while p is a point, the case below show the difference. * The same thing for them is both them can visit value by adding steps.*/ char s[] = "i love you"; /* s can not be refered to another address, but string content can be modified.*/ // s = "iii"; //this will cause a compile error, for s is not a pointer. *(s + 1) = 'i'; // this is well, because for an array, the value can be modified. printf("%s %c\n", s, *s); char *p = "i love you"; /* p can be refered to another address, but string content can not be modified. */ p = "you love me"; // this is well, because p is a point, not a name of array. // *(p + 1) = 'u'; // this will cause a runtime error, for p can not modify the value. printf("%s %c\n", p, *p); // char *p = "you love me"; p will be pointed to an anonymous string, so it can't modify the value. // s = p; s++; //this will cause a compile error, for s is not a pointer. p = s; /* p now refered to an array and can be used just like s to modify the string content.*/ *(p + 1) = ' '; printf("%s\n", p); // char *ss = "123"; This will cause a runtime error, for ss should be an array which its value is allowed to be modified. char ss[10] = "123"; char *tt = "456"; // Here, use pointer is ok, for we just visit the value, no modification. printf("%s compared with %s: %d\n", ss, tt, strcmp(ss, tt)); strcpy(ss, tt); strcpy1(ss, tt); strcpy2(ss, tt); strcpy3(ss, tt); strcpy4(ss, tt); printf("copy tt to ss get %s\n", ss); printf("%s compared with %s: %d\n", ss, tt, strcmp2(ss, tt)); }
int main(void) { char s[100]; char *t = "Test strcpy;"; strcpy1(s,t); printf("%s\n", t); strcpy2(s,t); printf("%s\n", t); strcpy3(s,t); printf("%s\n", t); strcpy4(s,t); printf("%s\n", t); strcpy1(s+strlen1(s),t); printf("%s\n", s); printf("%d\n", strcmp1(s,t)); printf("%d\n", strcmp2(s,t)); return 0; }