所以我在理解C中的指针时遇到了麻烦.我的问题是如何使char*find_ch_ptr返回char*.我也在整个地方遇到了演员问题.如果这里有问题,你能完整解释一下吗?
/*
* Return a pointer to the first occurrence of <ch> in <string>,
* or NULL if the <ch> is not in <string>.
*****
* YOU MAY *NOT* USE INTEGERS OR ARRAY INDEXING.
*****
*/
char *find_ch_ptr(char *string, char ch) {
char * point = (char*)(string + 0);
char * c = &ch;
while(point != '\0') {
if(point == c) return (char*)point;
else *point++;
}
return (char*)point; // placeholder
}
Run Code Online (Sandbox Code Playgroud)
while(point != '\0')
Run Code Online (Sandbox Code Playgroud)
应该
while(*point != '\0')
Run Code Online (Sandbox Code Playgroud)
有些地方你需要取消引用指针而你没有这样做.就像
while(*point != '\0')
{
if(*point == ch)
return point;
else
point ++;
}
Run Code Online (Sandbox Code Playgroud)
PS:point是指向某个有效内存位置的指针,通过解除引用来获取存储在该位置的值*point