#include<stdio.h> char fun(char *s,char *t) { int s1=0,t1=0; char *ss,*tt; ss=s;tt=t; /*******found*******/ while(*ss) { s1++; (*ss)++; } while(*tt) {t1++; (*tt)++; } if(t1<s1) return t; else return s; } void main() { char a[80],b[80]; printf("\nEnter a string:"); gets(a); printf("\nEnter a string:"); gets(b); printf("\nThe longer string is:%s",fun(a,b)); } }程序改错
时间: 2023-08-07 21:06:36 浏览: 101
下面是改错后的程序:
```c
#include<stdio.h>
char* fun(char *s,char *t) {
int s1=0,t1=0;
char *ss,*tt;
ss=s;
tt=t;
/*******found*******/
while(*ss) {
s1++;
(*ss)++;
}
while(*tt) {
t1++;
(*tt)++;
}
if(t1<s1)
return t;
else
return s;
}
int main() {
char a[80],b[80];
printf("\nEnter a string:");
gets(a);
printf("\nEnter a string:");
gets(b);
printf("\nThe longer string is:%s",fun(a,b));
return 0;
}
```
主要改动点如下:
1. `char fun(char *s,char *t)` 应该是 `char* fun(char *s,char *t)`,函数返回类型应该是字符指针类型。
2. `void main()` 应该是 `int main()`,main 函数的返回类型是整型。
3. `if(t1<s1) return t;` 应该是 `if(t1<s1) return t; else return s;`,因为函数返回的是字符指针类型,需要有 else 分支。
4. `gets()` 函数已经被弃用,应该使用 `fgets()` 函数替代。
5. 去掉了多余的右括号。
阅读全文
相关推荐
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![text/x-c](https://img-home.csdnimg.cn/images/20250102104920.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![image/gif](https://img-home.csdnimg.cn/images/20210720083352.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![exe](https://img-home.csdnimg.cn/images/20241231044909.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)