结构体指针的指针的问题

xiphias 2006-07-31 09:53:57
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

typedef struct tagTItem
{
char Name[10];
double Amt;
} TItem;

void getmem(TItem** item)
{
int i=0;
item = (TItem**)malloc(2*sizeof(TItem*));
if (item==NULL) { printf("malloc e.\n"); return; }
for (i=0; i<2; i++) {
*(item+i) = (TItem*)malloc(sizeof(TItem));
sprintf((*(item+i))->Name, "china%d", i+1);
(*(item+i))->Amt = (i+1)*12.0;
sprintf((*(item+i))->Name, "china%d", i+1);
(*(item+i))->Amt = (i+1)*12.0;
}
}

int main(int argc, char* argv[])
{
int i = 0, n = 0;
TItem **itm = NULL;
getmem(itm);
for(i=0; i<2; i++) {
printf("[%s]\n", (*itm++)->Name);
}
free(itm);
getchar();
return 0;
}


// 不知道哪里错了,打印的时候就core dump勒,帮忙看看,指针不会用啊
...全文
236 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
phoenixsharp 2006-08-01
  • 打赏
  • 举报
回复

/*
基本实现了楼主的要求,注意几个问题:
1、在传指针时一定要传地址,而不是传值,否则会少了一层.所以我多加了一层.
2、在函数getmem(TItem** item)中,item只是参数itm的一份拷贝,二者属于完全不同的变量。
3、为了方便理解,可另申请了一个变量,注释掉的部分。
4、C++ 支持使用&在参数中定义,可方便理解。
估计楼主想要测试的时在子函数中分配内存,然后想把指针带回来。
不过你这样用的指针层数太多了,实用性不强。
*/
#include "stdafx.h"
#include <stdio.h>
#include <malloc.h>
#include <string.h>

typedef struct tagTItem
{
int Amt;
} TItem;

void getmem(TItem ***item ){
int i=0;
//TItem **tt;
//tt = (TItem**)malloc(2*sizeof(TItem*));
*item = (TItem**)malloc(2*sizeof(TItem*));
// if (item==NULL) { printf("malloc e.\n"); return; }
for(i = 0 ; i<2; i++){
(*((*(item))+i)) = (TItem*)malloc(sizeof(TItem));
(*((*(item))+i))->Amt = 50 + i;
printf(" (*((*(item))+i))->Amt = %d \n", (*((*(item))+i))->Amt);
}
//*item = tt;
};
int main(int argc, char* argv[])
{
int i = 0, n = 0;
TItem **itm = NULL;
printf("&itm=%x\n", &itm);
getmem(&itm);
for(i=0; i < 2; i++)
printf("[%d]\n", (*itm++)->Amt);

//free(itm); //不能再free,itm已经飞了.
// getchar();
return 0;
}
xiphias 2006-08-01
  • 打赏
  • 举报
回复
嗯,明白勒,应该传指针的地址,而不是传指针的拷贝
感谢各位。
ddstudent 2006-07-31
  • 打赏
  • 举报
回复
这样写比较容易看懂

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

typedef struct tagTItem
{
char Name[10];
double Amt;
} TItem;

void getmem(TItem *item[])
{
int i=0;
for (i=0; i<2; i++)
{
item[i] = (TItem*)malloc(sizeof(TItem));
sprintf(item[i]->Name, "china%d", i+1);
item[i]->Amt = (i+1)*12.0;
// sprintf((*(item+i))->Name, "china%d", i+1); // 重复
// (*(item+i))->Amt = (i+1)*12.0; // 重复
}
}

int main(int argc, char* argv[])
{
int i = 0, n = 0;
TItem **itm = NULL;
itm = (TItem**)malloc(2*sizeof(TItem*));
if (itm==NULL)
{
printf("malloc e.\n");
return 0;
}
getmem(itm);
for(i=0; i<2; i++) {
printf("[%s]\n", itm[i]->Name); // itm+i
}
for(i = 0; i<2; i++)
{
free(itm[i]);
}
free(itm);
getchar();
return 0;
}
jixingzhong 2006-07-31
  • 打赏
  • 举报
回复
不要 定义 2级指针来分配,
定义1级指针传地址就是了 ...
jixingzhong 2006-07-31
  • 打赏
  • 举报
回复
void getmem(TItem** item)
{
int i=0;
*item = (TItem*)malloc(2*sizeof(TItem));
if (item==NULL) { printf("malloc e.\n"); return; }
for (i=0; i<2; i++)
{
//*(item+i) = (TItem*)malloc(sizeof(TItem));
sprintf((*item+i)->Name, "china%d", i+1);
(*item+i)->Amt = (i+1)*12.0;
}
}

int main(int argc, char* argv[])
{
int i = 0, n = 0;
TItem *itm = NULL;
getmem(&itm);
for(i=0; i<2; i++) {
printf("[%s]\n", (itm++)->Name);
}
free(itm);
getchar();
return 0;
}
ddstudent 2006-07-31
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

typedef struct tagTItem
{
char Name[10];
double Amt;
} TItem;

void getmem(TItem** item)
{
int i=0;
for (i=0; i<2; i++)
{
*(item+i) = (TItem*)malloc(sizeof(TItem));
sprintf((*(item+i))->Name, "china%d", i+1);
(*(item+i))->Amt = (i+1)*12.0;
// sprintf((*(item+i))->Name, "china%d", i+1); // 重复
// (*(item+i))->Amt = (i+1)*12.0; // 重复
}
}

int main(int argc, char* argv[])
{
int i = 0, n = 0;
TItem **itm = NULL;
// 程序最主要错在把 itm = malloc 放在函数里, 函数结束返回时並不会传回來
// itm 还是 NULL, 如果要在函数里改 itm 的值, 要传 &itm
itm = (TItem**)malloc(2*sizeof(TItem*));
if (itm==NULL)
{
printf("malloc e.\n");
return 0;
}
getmem(itm);
for(i=0; i<2; i++) {
printf("[%s]\n", (*(itm+i))->Name); // itm+i
}
free(itm);
getchar();
return 0;
}

69,373

社区成员

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

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