|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<string.h>
char *str_chr( char *s, int c)
{
while(*s)
{
if(*s == c)
{break;}
s++;
}
return s;
}
int main()
{
int c = 'c';
char str[128];
printf("输入字符串: ");
scanf("%s", str);
printf("字符串s中首个c的地址: %p\n", str_chr(str, c));
return 0;
}
#include<stdio.h>
#include<string.h>
char *str_chr(const char *s, int c)
{
while(*s)
{
if(*s == c)
{break;}
s++;
}
return s;
}
int main()
{
int c = 'c';
char str[128];
printf("输入字符串: ");
scanf("%s", str);
printf("字符串s中首个c的地址: %p\n", str_chr(str, c));
return 0;
}
第二种为什么运行不了呢?
把函数类型改成const char *
你的编译器将const char * 转换为 char* 的行为视为非法
不同编译器的实现规则多少会有差别,我这就根本不报错
最好的办法就是自己把代码写的完善些,类型不对的量主动将它们转换匹配好,比如传参的时候主动将str强制转换为const char *,而不是让编译器替你做
|
|