数据结构上的题目 不知道问题出再哪?
/*程序是数据结构上改的 */ #include <stdio.h>
#include <malloc.h>
#define NUM 80
#define LEN sizeof(STRING)
typedef struct string
{
int len;
char ch[NUM];
}STRING;
int StrCompare(STRING *s,STRING *t)
{
s=t=(STRING *)malloc(LEN);
int i=0;
while((s->ch[i]==t->ch[i])&&(s->ch[i]!='\0')&&(t->ch[i]!='\0'))
i++;
return s->ch[i]-t->ch[i];
}
int main()
{
STRING *s,*t;
s=t=(STRING *)malloc(LEN);
gets(s->ch);
gets(t->ch);
printf("%d",StrCompare(s,t));
getch();
return 0;
}
总是打印 0 ;是哪里出问题了
搜索更多相关的解决方案:
数据结构
----------------解决方案--------------------------------------------------------
#include <stdio.h>
#include <malloc.h>
#define NUM 80
#define LEN sizeof(STRING)
typedef struct string
{
int len;
char ch[NUM];
}STRING;
int StrCompare(STRING *s,STRING *t)
{
int i=0;
while((s->ch[i]==t->ch[i])&&(s->ch[i]!='\0')&&(t->ch[i]!='\0'))
i++;
return (int)(s->ch[i]-t->ch[i]);
}
int main()
{
STRING *s,*t;
t=(STRING *)malloc(LEN);
s=(STRING *)malloc(LEN);
gets(s->ch);
gets(t->ch);
printf("%d",StrCompare(s,t));
getchar();
return 0;
}
----------------解决方案--------------------------------------------------------
多谢啦 我知道我错哪了 执行函数后 又重新分配内存了 而且我还是把同一段内存赋给了两个不同的指针变量 所以出错了 我觉得编程要经常或者说每天练才行 像我这样就不行了 平均一个礼拜编不了一次 惭愧啊!!!!!!现在学数据结构 以前的c要继续复习巩固加强才行
----------------解决方案--------------------------------------------------------
呵呵,给你加油....
----------------解决方案--------------------------------------------------------