下列给定程序中,函数fun的功能是:比较两个字符串,将长的字符串的首地址作为函数值返回。 请改正程序中的错误,使它能得出正确的结果。 注意:部分源程序在文件MODI1.C中,不要改动main函数,不得增行或删行,也不得更改程序的结构! 试题程序: #inc

admin2020-07-28  34

问题 下列给定程序中,函数fun的功能是:比较两个字符串,将长的字符串的首地址作为函数值返回。
请改正程序中的错误,使它能得出正确的结果。
注意:部分源程序在文件MODI1.C中,不要改动main函数,不得增行或删行,也不得更改程序的结构!
试题程序:
#include<conio.h>
#include<stclio.h>
/*********found*********/
double fun(char*s,char*t)
{
  int s1=0,t1=0;
  char*ss,*tt;
    ss=s;
    tt=t;
    while(*ss)
    {
    s1++;
/*********found*********/
    (*ss)++;
  }
    while(*tt)
  {
    t1++;
/*********found*********/
    (*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 again:");
    gets(b);
    printf("\nThe longer is:\n\
n%S\n",fun(a,b));
}

选项

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

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

最新回复(0)