[求助]结构体指针问题!

ouleipeng 2008-10-28 09:37:28
struct Infor
{
char *name;
char *content;
Infor *next;
};

Infor* infor;
定义一个这样的指针,请问怎么给这个指针里的name,content初始化.
...全文
172 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
ouleipeng 2008-11-02
  • 打赏
  • 举报
回复
还有个问题,帮指教下.

怎样产生一个结构体链表.

还有,怎样访问这个链表.
hongyzniu 2008-10-31
  • 打赏
  • 举报
回复
#include <stdio.h> 

#include <stdlib.h>
struct Infor
{
char *name;
char *content;
Infor *next;
};
void main()
{
struct Infor* infor = (struct Infor*)malloc(sizeof(struct Infor));
infor->name = "hello";
infor->content="world";
infor->next = NULL;
free(infor);
}

楼主注意:指针一定要分配空间的!!
定义结构体指针后,先给结构体指针分配空间,然后再给结构体中的成员(也是指针)分配空间,切记哈!!
独行写的不错!
hngsc_0 2008-10-31
  • 打赏
  • 举报
回复
传指针
函数体外分配,用完释放
ouleipeng 2008-10-31
  • 打赏
  • 举报
回复
谢谢各位!
我想问下,如果把这个结构体传进一个函数里,是在函数里分配空间,还是在不函数外分配.
dz_huanbao 2008-10-29
  • 打赏
  • 举报
回复
楼主为什么不直接使用下面的定义:
struct Info
{
CString name;
CString content;
Info* next;
}

如果不支持MFC的话,不如将name和content定义为char[N]的形式,免得自己管理内存,既不方便,又容易导致内存泄露。
zhaohongbo83 2008-10-29
  • 打赏
  • 举报
回复
struct Infor* Infor_construct(const char *na, const char *con, struct Infor *ne)
{
struct Infor* infor = (struct Infor*)malloc(sizeof(struct Infor));
infor->name = NULL;
infor->content = NULL;
set_name(infor, na);
set_content(infor, con);
infor->next = ne;
return infor;
}
这个就很好!
Big鹏 2008-10-29
  • 打赏
  • 举报
回复

楼主注意:指针一定要分配空间的!!
定义结构体指针后,先给结构体指针分配空间,然后再给结构体中的成员(也是指针)分配空间,切记哈!!
独行写的不错!
budweiser 2008-10-28
  • 打赏
  • 举报
回复
这只是一个C型结构,直接复制就OK了
zhangxichao 2008-10-28
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Infor
{
char *name;
char *content;
Infor *next;
};
char* set_name(struct Infor* infor, const char *na)
{
if (infor->name != NULL)
free(infor->name);
infor->name = (char*)malloc(1+strlen(na));
strcpy(infor->name, na);
return infor->name;
}

char* set_content(struct Infor* infor, const char *cont)
{
if (infor->content != NULL)
free(infor->content);
infor->content = (char*)malloc(1+strlen(cont));
strcpy(infor->content, cont);
return infor->content;
}
struct Infor* Infor_construct(const char *na, const char *con, struct Infor *ne)
{
struct Infor* infor = (struct Infor*)malloc(sizeof(struct Infor));
infor->name = NULL;
infor->content = NULL;
set_name(infor, na);
set_content(infor, con);
infor->next = ne;
return infor;
}

void free_info(struct Infor *infor)
{
free(infor->name);
free(infor->content);
free(infor);
}
int main(void)
{
struct Infor* infor = Infor_construct("name1", "cont1", NULL);
printf("%s %s \n", infor->name, infor->content);
set_name(infor, "name2");
set_content(infor, "cont2");
printf("%s %s\n", infor->name, infor->content);
free_info(infor);
return 0;
}

太乙 2008-10-28
  • 打赏
  • 举报
回复
上面写的有点儿问题:
改改:

#include <stdio.h> 
#include <afx.h>
#include <stdlib.h>
struct Infor
{
char *name;
char *content;
Infor *next;
};
void main()
{
struct Infor* infor = (struct Infor*)malloc(sizeof(struct Infor));
CString cs="name",strr = "content";

infor->name = (char*)malloc(sizeof(char)*(cs.GetLength()+1));
infor->content= (char*)malloc(sizeof(char)*(strr.GetLength()+1));
strcpy(infor->name,cs);
strcpy(infor->content,strr);
infor->next = NULL;
printf("%s,%s",infor->name,infor->content);
free(infor->name);
free(infor->content);
free(infor);
}
fengyizi 2008-10-28
  • 打赏
  • 举报
回复
你用c++里的string就不用strcpy那些函数了。
太乙 2008-10-28
  • 打赏
  • 举报
回复
#include <stdio.h> 
#include <afx.h>
#include <stdlib.h>
struct Infor
{
char *name;
char *content;
Infor *next;
};
void main()
{
struct Infor* infor = (struct Infor*)malloc(sizeof(struct Infor));
CString cs="name",strr = "content";

infor->name = (char*)malloc(sizeof(char)*cs.GetLength());
infor->content= (char*)malloc(sizeof(char)*strr.GetLength());
strcpy(infor->name,cs);
strcpy(infor->content,strr);
infor->next = NULL;
printf("%s,%s",infor->name,infor->content);

free(infor);
}
太乙 2008-10-28
  • 打赏
  • 举报
回复
#include <stdio.h> 

#include <stdlib.h>
struct Infor
{
char *name;
char *content;
Infor *next;
};
void main()
{
struct Infor* infor = (struct Infor*)malloc(sizeof(struct Infor));
infor->name = "hello";
infor->content="world";
infor->next = NULL;
free(infor);
}
Jncryang 2008-10-28
  • 打赏
  • 举报
回复
#include <string>
using namespace std;
struct Infor
{
char *name;
char *content;
Infor *next;
};
int main()
{
Infor* infor;
infor = new Infor;
string str= "name";
string strr="content";

infor->name = new char[str.length() + 1];
strcpy(infor->name, str.c_str());

infor->content = new char[strr.length() + 1];
strcpy(infor->content, strr.c_str());
return 0;
}
ouleipeng 2008-10-28
  • 打赏
  • 举报
回复
我的意思是:
CString str= "name";
CStirng strr="content";

怎么赋给结构体的name 和content
wjb_yd 2008-10-28
  • 打赏
  • 举报
回复
净瞎说
ouleipeng 2008-10-28
  • 打赏
  • 举报
回复
怎样给它赋值?
开心爸爸84 2008-10-28
  • 打赏
  • 举报
回复
Infor* infor = new Infor{null, null, null};

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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