#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef struct
{
char *ch;
int length;
}HString;
typedef int Status;
Status StrAssign(HString &T,char *chars)
{
int i,j;
char *c;
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];
T.length=i;
}
return OK;
}
void main()
{
HString str;
StrAssign(str,"HELLO WORLD!");
puts(str.ch);
}
用堆存储结构,就是一个简单的赋值函数,调试的时候出错(Debug assert failed.)
出错是什么dbgheap.c line:1011
总之看不懂,又不知道怎么解决。函数是书上抄的……应该是没问题的。
|