#include <stdio.h>
#include <string.h>
// #include <stdlib.h> //if open ,error occur.
typedef struct stu
{
char *name;
int soc;
int ll;
};int main()
{
struct stu st;
st.name = ( char *)malloc(sizeof( struct stu)); //if struct use '->',error occur!!!!
strcpy(st.name,"asfghjkll ");
st.soc=99;
printf("st.name is %s\n",st.name);
printf("st.soc is %d\n",st.soc);
printf("st.soc is %d\n",sizeof( st)); //size of st.name is 4.in vc++6.0.free(st.name);
return 0;
}
为结构体成员分配内存(vc++6.0调试)
最新推荐文章于 2022-11-21 15:46:50 发布
本文探讨了C语言中结构体的使用与内存管理,包括结构体变量的声明、成员的初始化、内存分配及释放。通过实例展示了如何利用malloc动态分配内存给结构体成员,并在使用完毕后正确释放内存避免泄漏。
1043

被折叠的 条评论
为什么被折叠?



