65,210
社区成员
发帖
与我相关
我的任务
分享#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);
}
楼主注意:指针一定要分配空间的!!
#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;
}
#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);
}
#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);
}
#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);
}
Infor* infor = new Infor{null, null, null};