当前位置: 技术问答>linux和unix
puts函数出问题
来源: 互联网 发布时间:2016-07-09
本文导语: #include #include #include #include #include typedef struct{ char *ch; int len; }HString; void StrAssign(HString *T,char *chars){ int i = 0; T = (HString*)malloc(sizeof(HString)*10); if(T->ch!=NULL) free(T->ch); if(!T->ch); T->ch = (char*)malloc(10*sizeof(char)); i = s...
#include
#include
#include
#include
#include
typedef struct{
char *ch;
int len;
}HString;
void StrAssign(HString *T,char *chars){
int i = 0;
T = (HString*)malloc(sizeof(HString)*10);
if(T->ch!=NULL)
free(T->ch);
if(!T->ch);
T->ch = (char*)malloc(10*sizeof(char));
i = strlen(chars);
if(!i){
T->ch = NULL;
T->len = 0;
}
else{
T->len = i;
T->ch[i+1] = '';
for(;i>=0;i--){
T->ch[i] = chars[i];
}
}
}
int main()
{
HString *T;
HString *S1;
HString *S2;
int i;
char *tmp=NULL;
tmp = (char*)malloc(sizeof(char));
printf("Please input your datan");
gets(tmp);
StrAssign(T,tmp);
puts(T->ch);
}
程序执行到puts后退出了
为什么。。。。
|
在StrAssign(T,tmp);之间加一行
T = (HString*)malloc(sizeof(HString));
然后把StrAssign函数里面的这行删掉T = (HString*)malloc(sizeof(HString)*10);
T = (HString*)malloc(sizeof(HString));
然后把StrAssign函数里面的这行删掉T = (HString*)malloc(sizeof(HString)*10);