当前位置: 技术问答>linux和unix
给T->ch分配空间是出现段错误 (gcc下)
来源: 互联网 发布时间:2016-07-08
本文导语: 怎么回事。。。。。找了好久资料还是没找到错哪了。。。 #include #include #include typedef struct{ char *ch; int len; }HString; int main() { HString *T; T->ch = (char*)malloc(sizeof(char)*8); gets(T->ch); int i = 0; i = strlen(T->ch); printf...
怎么回事。。。。。找了好久资料还是没找到错哪了。。。
#include
#include
#include
typedef struct{
char *ch;
int len;
}HString;
int main()
{
HString *T;
T->ch = (char*)malloc(sizeof(char)*8);
gets(T->ch);
int i = 0;
i = strlen(T->ch);
printf("%dn",i);
puts(T->ch);
free(T->ch);
}
#include
#include
#include
typedef struct{
char *ch;
int len;
}HString;
int main()
{
HString *T;
T->ch = (char*)malloc(sizeof(char)*8);
gets(T->ch);
int i = 0;
i = strlen(T->ch);
printf("%dn",i);
puts(T->ch);
free(T->ch);
}
|
#include
#include
#include
typedef struct{
char *ch;
int len;
}HString;
int main()
{
HString *T=(HString *)malloc(sizeof(HString));
T->ch = (char*)malloc(sizeof(char)*8);
gets(T->ch);
int i = 0;
i = strlen(T->ch);
printf("%dn",i);
puts(T->ch);
free(T->ch);
}
#include
#include
typedef struct{
char *ch;
int len;
}HString;
int main()
{
HString *T=(HString *)malloc(sizeof(HString));
T->ch = (char*)malloc(sizeof(char)*8);
gets(T->ch);
int i = 0;
i = strlen(T->ch);
printf("%dn",i);
puts(T->ch);
free(T->ch);
}