C指针的问题
Problems with C pointers
所以我一直无法理解 C 中的指针。我的问题是如何使 char * find_ch_ptr return 成为 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 != '[=10=]') {
if(point == c) return (char*)point;
else *point++;
}
return (char*)point; // placeholder
}
while(point != '[=10=]')
应该是
while(*point != '[=11=]')
有些地方你需要取消引用指针,但你没有这样做it.Like
while(*point != '[=12=]')
{
if(*point == ch)
return point;
else
point ++;
}
PS: point
是指向某个有效内存位置的指针,存储在该位置的值是通过取消引用它获得的 *point
试试 strchr()
用法:
#include <string.h>
ptr = strchr( s, c );
要比较 point
当前指向的字符,您需要使用 *
运算符取消引用 point
,就像这样
while (*point != '[=10=]')
然后你想比较你正在搜索的字符,但你这样做的方式也错误。
您正在将变量 ch
的地址与当前 point
指向的地址进行比较,您需要的是错误的
if (*point == ch)
相反。
这是您的代码的工作版本
请老师告诉您,您是从堆栈溢出中得到的
char *find_ch_ptr(char *string, char ch) {
while(*string != '[=10=]') {
if(*string == ch)
return string;
else string++;
}
return (char*)0;
}
或 K&R 方式
while(*string && *string!=ch);
return str;
在检查等价“\0”之前,需要取消引用 while 循环条件。
不需要c变量,while循环内部的检查可以直接使用ch参数
这是一个工作示例:
#include<stdio.h>
char *find_ch_ptr(char *string, char ch) {
char *point = string;
while(*point != '[=10=]') {
if(*point == ch)
{
return point;
}
else
{
point++;
}
}
return point; // placeholder
}
int main(int argc, char* argv[]){
printf("%c\n", *find_ch_ptr("hello world", 'r'));
return 0;
}
所以我一直无法理解 C 中的指针。我的问题是如何使 char * find_ch_ptr return 成为 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 != '[=10=]') {
if(point == c) return (char*)point;
else *point++;
}
return (char*)point; // placeholder
}
while(point != '[=10=]')
应该是
while(*point != '[=11=]')
有些地方你需要取消引用指针,但你没有这样做it.Like
while(*point != '[=12=]')
{
if(*point == ch)
return point;
else
point ++;
}
PS: point
是指向某个有效内存位置的指针,存储在该位置的值是通过取消引用它获得的 *point
试试 strchr()
用法:
#include <string.h>
ptr = strchr( s, c );
要比较 point
当前指向的字符,您需要使用 *
运算符取消引用 point
,就像这样
while (*point != '[=10=]')
然后你想比较你正在搜索的字符,但你这样做的方式也错误。
您正在将变量 ch
的地址与当前 point
指向的地址进行比较,您需要的是错误的
if (*point == ch)
相反。
这是您的代码的工作版本
请老师告诉您,您是从堆栈溢出中得到的
char *find_ch_ptr(char *string, char ch) {
while(*string != '[=10=]') {
if(*string == ch)
return string;
else string++;
}
return (char*)0;
}
或 K&R 方式
while(*string && *string!=ch);
return str;
在检查等价“\0”之前,需要取消引用 while 循环条件。
不需要c变量,while循环内部的检查可以直接使用ch参数
这是一个工作示例:
#include<stdio.h>
char *find_ch_ptr(char *string, char ch) {
char *point = string;
while(*point != '[=10=]') {
if(*point == ch)
{
return point;
}
else
{
point++;
}
}
return point; // placeholder
}
int main(int argc, char* argv[]){
printf("%c\n", *find_ch_ptr("hello world", 'r'));
return 0;
}