이 코드를 읽는 데 디버거를 사용하고 있는데 약간 혼동 스럽습니다. while ((*d++ = *s2++));
- 디버거 변수에서 d가 각 루프 이후에 짧아지는 것 같습니다 'Hello hello'
에서 'ello hello'
까지이고 s1
은 'cello hello'
으로 바뀝니다. 반복 루프 루핑이란 무엇입니까? while(condition); do(something))
?C :이 코드의 while 루프에서 포인터를 이해하는 데 문제가 없습니다.
d와 s1의 변수 값이 같지 않은 이유는 무엇입니까 (d는 s1에 대한 포인터가 아닙니까)? ? 주요 기능은,이 strcpy()
의 다소 전형적인 교훈 구현
/*
Input: char pointers for source (s2) and destination (s1)
Output: returns the pointer to the destination (s1)
*/
char *my_strcpy(char * , const char *);
int main()
{
char src[] = "cs23!";
char dst[]="Hello hello";
char *curdst;
int len=0;
while(src[len++]);
// do the copy
curdst= my_strcpy(dst, src);
// check to see if the NULL char is copied too.
printf("dst array %s and last element %d\n", dst, atoi(&dst[len]));
return 0;
}
char *my_strcpy(char *s1, const char *s2) {
register char *d = s1;
// print the pointer variables address and their contents, and first char
printf("s2 address %p, its contents is a pointer %p to first char %c \n", (void *)&s2, (void *)s2, *s2);
printf("s1 address %p, its contents is a pointer %p to first char %c \n", (void *)&s1, (void *)s1, *s1);
while ((*d++ = *s2++));
return(s1);
}
'* D * S2 = ++ ++'매우 다르다 '* D * S2 == ++ ++'. 여기서's2'의 널 종결자가 될 때까지'* d2 '의 값을'* s2'의 값으로 덮어 쓰게됩니다. – hnefatl
조건은'* s2'가 0 (문자열 널 종결 자)이 아닌 것입니다. –
유사한 것의 분류에 대해서는 [이 답변] (https://stackoverflow.com/a/20957532/2201041)을 참조하십시오. – JETM