级指针如何释放

nuoshueihe nuoshueihe 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct STUTAG
{
int age;
char name[100];
char addr[40];
}STU;

int main()
{
STU* pt1 = (STU*)malloc(sizeof(STU)*2);
//通过方式一赋值
if(pt1 != NULL)
{
pt1[0].age = 10;
strcpy(pt1[0].name,"this is test");
pt1[1].age = 20;
strcpy(pt1[1].name,"this is xxxtest");
}
//通过方式2赋值
/*
if(pt1 != NULL)
{
pt1->age = 10;
strcpy(pt1->name,"this is test");
pt1 ++;
pt1->age = 10;
strcpy(pt1->name,"this is testnsn");
pt1--;
}
*/

STU** pt2 = (STU**)malloc(sizeof(STU*)*2);
pt2[0] = pt1;
pt1++;
pt2[1] = pt1;
pt1--;
printf("pt2[0]: pt2[0]->name %s\n", pt2[0]->name);
printf("pt2[1]: pt2[1]->name %s\n", pt2[1]->name);
free(pt1);
//到底如何释放pt2呢?我们项目里面都是free(&pt2)使用,但是我测试发现会出core,这样释放返回没有问题free(&pt2[0]),不得期解,望各位高手指教
//free(&pt2)/free(&pt2[0]);
return 0;


...全文
评论 4
收藏 已收藏
分享
返回顶部
nuoshueihe
C语言 70,039 成员 243,237 内容