C语言问题,急。谢谢

来源:百度知道 编辑:UC知道 时间:2024/06/24 01:55:28
谁帮我看一下这些代码
编译是没有错误,但运行不了,

题目是:比较两个字符串

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
#define Max 1000
typedef struct
{
char *ch;
int length;
}DString;
int Compare(DString *S,DString *T)
{
int i;
if(S->length>T->length)
{
printf("S>T\n");
exit(1);
}
else
if(S->length<T->length)
{
printf("S<T\n");
exit(1);
}
else
for(i=0;i<S->length;i++)
{
if(S->ch[i]==T->ch[i])
i++;
else
{
printf("两字符串不相等");
exit(1);
}
}
printf("两字符串相等");
}

void main()
{
DString *S,*T;
S-&g

两个错误:
1. Compare函数类型为int型,但是没有返回值
2. S和T应该使用结构体变量,而不是结构体指针变量。

下面的程序已经编译运行确认了,
具体的看一下注释吧:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
#define Max 1000
typedef struct
{
char *ch;
int length;
}DString;
int Compare(DString *S,DString *T)
{
int i;
if(S->length>T->length)
{
printf("S>T\n");
exit(1);
}
else
if(S->length<T->length)
{
printf("S<T\n");
exit(1);
}
else
for(i=0;i<S->length;i++)
{
if(S->ch[i]==T->ch[i])
i++;
else
{
printf("两字符串不相等");
exit(1);
}
}
printf("两字符串相等");
return 1;//增加返回值,这个返回值没什么意义,就是为了配合函数的返回类型的
}

void main()
{
DString S,T; //将S,T声明为结构体变量
S.ch=(char *)mall