使用VC++2010打开考生文件夹下modil中的解决方案。此解决方案的项目中包含一个源程序文件modil.c。在此程序中,函数fun()的功能是比较两个字符串,将长的字符串的首地址作为函数值返回。 请改正程序中的错误.使它能得出正确的结果。

admin2023-02-27  2

问题 使用VC++2010打开考生文件夹下modil中的解决方案。此解决方案的项目中包含一个源程序文件modil.c。在此程序中,函数fun()的功能是比较两个字符串,将长的字符串的首地址作为函数值返回。
    请改正程序中的错误.使它能得出正确的结果。
    注意:部分源程序在文件modi1.c中,不要改动main()函数,不得增行或删行,也不得更改程序的结构!
    试题程序:
1 #include<conio.h>
2 #include<stdio.h>
3 /*********found*********/
4 double fun(char*s,char*t)
5 {
6   int s1=0,t1=0;
7  char*ss,*tt;
8  ss=s;
9  tt=t;
10  while(*ss)
11  {
12    s1++;
13    /*********found*********/
14    (*ss)++;
15  }
16    while(*tt)
17  {
18    t1++;
19  /*********found*********/
20    (*tt)++;
21  }
22  if(t1>s1)
23    return t;
24  else
25    return s;
26  }
27 void main()
28 {
29 char a[80],b[80];
30  printf("\nEnter a string:");
31  gets(a);
32  printf("\nEnter a stringagain:");
33  gets(b);
34  printf("\nThe longer is:\n\n%s\n",fun(a,b));
35  }

选项

答案(1)char*fun(char*s,char*t) (2)ss++: (3)tt++;

解析 (1)在主函数的输出语句中,函数fun()以字符串格式%s输出。所以定义函数时应为char*fun(char*s,char*t)。
    (2)和(3)这里是地址加1,而不是内容加1,所以应改为ss++;和tt++;。  
转载请注明原文地址:https://kaotiyun.com/show/ED0D777K
0

最新回复(0)