指针12 返回指针的函数

函数调用结束后,可以返回一个值给主函数,这个值可以是整型、实型、字符型等基本类型数据,也可以是指针,即地址。

例如
题目:编写str_chr函数,其功能是在一个字符串中查找一个指定字符,找到后返回该字符的地址,若未找到则返回空指针。在main函数中调用str_chr 函数,查找字符‘e’在字符串“How are you!”中的地址以及相对位置,并输出。
代码如下

#include<stdio.h>
char*str_chr(char*str,char ch)
{while((*str!=ch)&&(*str!=NULL))
  str++;
if(*str==NULL)
{return (0);}
return (str);
} 
int main()
{char*p,ch,str[]="How are you!";
 ch='e';
 p=str_chr(str,ch);
 if(p)
 {printf("string starts at %xh.\n",str);
  printf("char\'%c\'at%xh.\n",ch,p);
  printf("position is %d.\n",p-str);
 }
 else 
	 printf("not found!\n");
 return 0;
}

运行结果
在这里插入图片描述

楼主要赶紧学习链码了,工作室催得急。。。
下一篇的c可能告一段落了 如果有 那就是
指针13 指向函数的指针

猜你喜欢

转载自blog.csdn.net/weixin_43918004/article/details/85048778