[求助]指针的困扰
#include<stdio.h> char *strchr(const char *,int letter);
main()
{
char *test="this is the end of the test.";
char (*tempptr)(const char *,int);
char *pointer;
tempptr=strchr;
pointer=tempptr(test,'i');
pointer=strchr(test,'i');
if(*pointer)
{printf("The first of i is at %d\n",pointer-test);
}
else printf("The character is not found.\n");
}
char *strchr(const char *string,int letter)
{ while((*string!=letter)&&(*string))
string++;
return string;
}
这个程序是我在一本书上看到的,在turboC3.0下编译通过,但得不到预期的结果.
后来把 char *test="this is the end of the test.";
改为 char test[]="this is the end of the test.";
就得到正确结果.
怎么会这样呢?各位高手,帮帮忙啊!
----------------解决方案--------------------------------------------------------
[URL=http://home.jznu.net/fc_snowly/point/web_course/view.asp?id=74]http://home.jznu.net/fc_snowly/point/web_course/view.asp?id=74[/URL]
----------------解决方案--------------------------------------------------------
明白了.谢谢啊!
----------------解决方案--------------------------------------------------------
用指针引用字符串 前提指针以指向以存在 并足够的空间
----------------解决方案--------------------------------------------------------