struct问题

hanmm9 2006-06-12 02:52:36
在dev-c++里建了一个工程,有两个.c文件。

//////////test1.c ////////

struct st{
int i;
char *str;
};

struct st *np;
struct st *init(){
if((np=(struct st*)malloc(sizeof(struct st)))==NULL)
return NULL;
np->name=(char *)malloc(12);
strcpy(np->str,"hello");
np->i=12;

return np;
}

///////testmain.c///////////////

struct st *init();
main(){
printf("%s",init()->name);
system("pause");
}

编译报错,dereferencing pointer to incomplete type

请各位大虾指教,怎么改。。谢谢

...全文
174 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
hanmm9 2006-06-12
  • 打赏
  • 举报
回复
问题解决,谢谢各位
duduhaha 2006-06-12
  • 打赏
  • 举报
回复
同意楼上的说法,呵呵.struct st没有声明.
tailzhou 2006-06-12
  • 打赏
  • 举报
回复
你的 ///////testmain.c/////////////// 文件内都没struct st的任何定义与说明,怎么能用??

你缺头文件吧??
goodluckyxl 2006-06-12
  • 打赏
  • 举报
回复
我看不出什么问题
hanmm9 2006-06-12
  • 打赏
  • 举报
回复
难道是编译器的问题?

有没有dev-c++的高手阿,应该怎么改
hanmm9 2006-06-12
  • 打赏
  • 举报
回复
对不起大家了,写错了。。
应该是这样
//////////test1.c ////////

struct st{
int i;
char *str;
};

struct st *np;
struct st *init(){
if((np=(struct st*)malloc(sizeof(struct st)))==NULL)
return NULL;
np->str=(char *)malloc(12);
strcpy(np->str,"hello");
np->i=12;

return np;
}

///////testmain.c///////////////

struct st *init();
main(){
printf("%s",init()->str);
system("pause");
}

还是会出现错误,dereferencing pointer to incomplete type
HxIn_it 2006-06-12
  • 打赏
  • 举报
回复
struct st
{
int i;
char* str;
};

struct st *np;
struct st *init()
{
if((np = (struct st*)malloc(sizeof(struct st))) == NULL)
return NULL;

np ->str = (char*)malloc(12);
strcpy(np ->str,"hello");
np ->i = 12;

return np;
}

///////testmain.c///////////////


main()
{
printf("%s",init() ->str);
system("pause");
}

在我的VS2003里可以编译通过,显示Hello.
minioreo 2006-06-12
  • 打赏
  • 举报
回复
np->name=(char *)malloc(12);
你的这句话有点问题哈 np是指向结构体的 可是结构体里根本就没有name这个成员哈
heroooooo 2006-06-12
  • 打赏
  • 举报
回复
if((np=(struct st*)malloc(sizeof(struct st)))==NULL)

np是个什么东西??
hanmm9 2006-06-12
  • 打赏
  • 举报
回复
不好意思,写错了,改成init()->str也不好用
triace_zhang 2006-06-12
  • 打赏
  • 举报
回复
init()->name?
你的struct里不是str吗?
改成init()->str

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧