写了半天的程序(有关数据结构里的串的问题),结果有错误,并且不能运行,求指点
head.htypedef struct
{
char *ch;
int length;
}
Status StrAssign(HString&T,char*chars)
{
if(T.ch) free(T.ch);
for(i=0,c=chars; *c; ++i,++c);
if(!i)
{
T.ch=NULL; T.length=0;
}
else
{
if(!(T.ch=(char*)malloc(i*sizeof(char))))
exit(OVERFLOW);
for(j=0;j<i;j++)
T.ch[j]=chars[j];
}
return OK;
}
Status SubString(HString&Sub,HString S,int pos, int len)
{
if(pos<1||pos>S.length||len<0||len>S.length-pos+1)
return ERROR;
if(Sub.ch) free(Sub.ch);
if(!len)
{
Sub.ch=NULL;
Sub.length=0;
}
else
{
Sub.ch=(char*)malloc(len*sizeof(char));
for(j=0;j<len;j++)
for(i=pos-1;i<pos+len-1;i++)
Sub.ch[j]=S.ch[i];
}
return OK;
}
Status Concat(HString&T,HStringS1,HStringS2)
{
if(T.ch) free(T.ch);
if(!(T.ch=(char*)malloc((S1.length+S2.length)*sizeof(char))))
exit(OVERFLOW);
for(j=0;j<S.length;j++)
for(i=0;i<S1.length;i++)
T.ch[j]=S1.ch[i];
T.length=S1.length+S2.length;
for(k=S1.Length;k<T.length;k++)
for(p=0;p<S2.length;p++)
T.ch[k]=S2.ch[p];
return OK;
}
.cpp文件
#include<stdio.h>
#include <stdlib.h>
#include <iostream.h>
#define true 1
#define false 0
#define ERROR 0
#define OVERFLOW -2
#include<head.h>
void main()
{
Status StrAssign(HString&S1,char*THIS IS A BOOK)
printf("S1=%c",S1);
}