结构体指针的指针的问题 c-c++ - c语言 - csdn社区 community_csdn_net.htm
来自「最全的介绍C语言结构体的使用方法和使用技巧!」· HTM 代码 · 共 348 行 · 第 1/2 页
HTM
348 行
//*(item+i) = (TItem*)malloc(sizeof(TItem));
<BR> sprintf((*item+i)->Name, "china%d",
i+1); <BR> (*item+i)->Amt =
(i+1)*12.0; <BR> } <BR> }
<BR> <BR> int main(int argc, char*
argv[]) <BR> { <BR> int i
= 0, n = 0; <BR>
TItem *itm = NULL; <BR>
getmem(&itm); <BR> for(i=0; i<2;
i++) { <BR>
printf("[%s]\n", (itm++)->Name); <BR> }
<BR> free(itm); <BR>
getchar(); <BR> return 0; <BR>
}<A href="http://topic.csdn.net/t/20060731/21/4917799.html#">Top</A></P>
<H3><STRONG><A class=anchor name=r_35893439>3 楼</A>jixingzhong(瞌睡虫·星辰)<INPUT class=star4 title="四星用户 该版得分小于等于100000分,大于50000分" type=button></STRONG><SPAN>回复于
2006-07-31 22:17:58 得分 0 </SPAN></H3>
<P>不要 定义 2级指针来分配, <BR> 定义1级指针传地址就是了 ...<A
href="http://topic.csdn.net/t/20060731/21/4917799.html#">Top</A></P>
<H3><STRONG><A class=anchor name=r_35893449>4 楼</A>ddstudent()<INPUT class=user2 title="二级用户 该版得分小于等于500分,大于100分" type=button></STRONG><SPAN>回复于
2006-07-31 22:19:00 得分 0 </SPAN></H3>
<P>这样写比较容易看懂 <BR> <BR> #include <stdio.h>
<BR> #include <stdlib.h> <BR> #include
<assert.h> <BR> #include <string.h>
<BR> <BR> typedef struct tagTItem
<BR> { <BR> char Name[10];
<BR> double Amt; <BR> } TItem;
<BR> <BR> void getmem(TItem *item[])
<BR> { <BR> int i=0;
<BR> for (i=0; i<2; i++)
<BR> { <BR>
item[i] = (TItem*)malloc(sizeof(TItem)); <BR>
sprintf(item[i]->Name, "china%d", i+1);
<BR> item[i]->Amt =
(i+1)*12.0; <BR> //
sprintf((*(item+i))->Name, "china%d", i+1);
// 重复 <BR> //
(*(item+i))->Amt = (i+1)*12.0;
// 重复 <BR> } <BR> }
<BR> <BR> int main(int argc, char*
argv[]) <BR> { <BR> int i
= 0, n = 0; <BR>
TItem **itm = NULL; <BR>
itm = (TItem**)malloc(2*sizeof(TItem*));
<BR> if (itm==NULL) <BR>
{ <BR> printf("malloc
e.\n"); <BR> return
0; <BR> } <BR>
getmem(itm); <BR> for(i=0; i<2;
i++) { <BR>
printf("[%s]\n", itm[i]->Name);
// itm+i <BR> }
<BR> for(i = 0; i<2; i++)
<BR> { <BR>
free(itm[i]); <BR> } <BR>
free(itm); <BR> getchar(); <BR>
return 0; <BR> } <BR> <A
href="http://topic.csdn.net/t/20060731/21/4917799.html#">Top</A></P>
<H3><STRONG><A class=anchor name=r_35894636>5 楼</A>phoenixsharp(小星星)<INPUT class=user1 title="一级用户 该版得分小于等于100分" type=button></STRONG><SPAN>回复于
2006-08-01 00:53:28 得分 <EM>5</EM></SPAN></H3>
<P> <BR> /* <BR> 基本实现了楼主的要求,注意几个问题: <BR>
1、在传指针时一定要传地址,而不是传值,否则会少了一层.所以我多加了一层. <BR>
2、在函数getmem(TItem** item)中,item只是参数itm的一份拷贝,二者属于完全不同的变量。
<BR> 3、为了方便理解,可另申请了一个变量,注释掉的部分。 <BR> 4、C++
支持使用&在参数中定义,可方便理解。 <BR>
估计楼主想要测试的时在子函数中分配内存,然后想把指针带回来。 <BR> 不过你这样用的指针层数太多了,实用性不强。
<BR> */ <BR> #include "stdafx.h"
<BR> #include <stdio.h> <BR> #include
<malloc.h> <BR> #include <string.h>
<BR> <BR> typedef struct tagTItem
<BR> { <BR> int Amt;
<BR> } TItem; <BR> <BR> void
getmem(TItem ***item ){ <BR> int
i=0; <BR> //TItem **tt;
<BR> //tt =
(TItem**)malloc(2*sizeof(TItem*)); <BR> *item
= (TItem**)malloc(2*sizeof(TItem*)); <BR>
// if (item==NULL) { printf("malloc
e.\n"); return; } <BR> for(i
= 0 ; i<2; i++){ <BR>
(*((*(item))+i)) = (TItem*)malloc(sizeof(TItem));
<BR>
(*((*(item))+i))->Amt = 50 + i;
<BR> printf("
(*((*(item))+i))->Amt = %d \n",
(*((*(item))+i))->Amt); <BR> } <BR>
//*item = tt; <BR> };
<BR> int main(int argc, char* argv[])
<BR> { <BR> int i =
0, n = 0; <BR> TItem
**itm = NULL; <BR>
printf("&itm=%x\n", &itm); <BR>
getmem(&itm); <BR> for(i=0; i <
2; i++) <BR> printf("[%d]\n",
(*itm++)->Amt); <BR>
<BR> //free(itm); //不能再free,itm已经飞了.
<BR> // getchar(); <BR> return
0; <BR> } <BR> <A
href="http://topic.csdn.net/t/20060731/21/4917799.html#">Top</A></P>
<H3><STRONG><A class=anchor name=r_35898578>6 楼</A>xiphias(var)<INPUT class=user1 title="一级用户 该版得分小于等于100分" type=button></STRONG><SPAN>回复于
2006-08-01 10:16:42 得分 0 </SPAN></H3>
<P>嗯,明白勒,应该传指针的地址,而不是传指针的拷贝 <BR> 感谢各位。<A
href="http://topic.csdn.net/t/20060731/21/4917799.html#">Top</A></P>
<H4><STRONG>相关问题</STRONG></H4>
<DIV class=relation></DIV></DIV></DIV>
<DIV id=sidebar>
<H3>关键词</H3>
<DIV></DIV>
<H3>得分解答快速导航</H3>
<DIV>
<UL>
<LI>帖主:<A
href="http://topic.csdn.net/t/20060731/21/4917799.html#Top">xiphias</A>
<LI><A
href="http://topic.csdn.net/t/20060731/21/4917799.html#r_35893378">ddstudent</A>
<LI><A
href="http://topic.csdn.net/t/20060731/21/4917799.html#r_35893430">jixingzhong</A>
<LI><A
href="http://topic.csdn.net/t/20060731/21/4917799.html#r_35894636">phoenixsharp</A>
</LI></UL></DIV>
<H3>相关链接</H3>
<DIV>
<UL>
<LI><A href="http://blog.csdn.net/AggView/35/" target=_blank>C/C++ Blog</A>
<LI><A
href="http://www.dearbook.com.cn/Book/SearchBook.aspx?sortid=7&sorttype=smallsort"
target=_blank>C/C++类图书</A>
<LI><A href="http://www.codechina.net/resource/sort.php/20"
target=_blank>C/C++类源码下载</A> </LI></UL></DIV>
<H3>广告也精彩</H3>
<DIV>
<SCRIPT
src="结构体指针的指针的问题 C-C++ - C语言 - CSDN社区 community_csdn_net.files/show_ads.js"
type=text/javascript></SCRIPT>
</DIV>
<H3>反馈</H3>
<DIV>请通过下述方式给我们反馈<BR><IMG alt=反馈
src="结构体指针的指针的问题 C-C++ - C语言 - CSDN社区 community_csdn_net.files/feedback.gif"></DIV></DIV>
<DIV class=clear></DIV></DIV>
<DIV class=CSDN-PHF id=CSDNPF>
<HR>
<A class=biaoshi
href="http://www.hd315.gov.cn/beian/view.asp?bianhao=010202001032100010"
rel=external> </A>
<DIV><A href="http://www.csdn.net/intro/intro.asp?id=2" rel=external>网站简介</A>-<A
href="http://www.csdn.net/intro/intro.asp?id=5" rel=external>广告服务</A>-<A
href="http://www.csdn.net/map/map.shtm" rel=external>网站地图</A>-<A
href="http://www.csdn.net/help/help.asp" rel=external>帮助</A>-<A
href="http://www.csdn.net/intro/intro.asp?id=9" rel=external>联系方式</A>-<A
href="http://job.csdn.net/Jobs/f9c75c9f2ad14404a604669b757b9ed0/viewcompany.aspx"
rel=external>诚聘英才</A>-<A href="http://www.csdn.net/english/"
rel=external>English</A>-<A
href="javascript:navigate('mai'%20+%20'lto:'%20+%20'webm'%20+%20'aster@c'%20+%20'sdn.n'+'et?subject=向CSDN报告问题')"
rel=external>问题报告</A></DIV>
<DIV>北京百联美达美数码科技有限公司 版权所有 京 ICP 证 020026 号</DIV>
<DIV>Copyright © 2000-2006, CSDN.NET, All Rights Reserved</DIV>
<SCRIPT
src="结构体指针的指针的问题 C-C++ - C语言 - CSDN社区 community_csdn_net.files/counter.js"
type=text/javascript></SCRIPT>
<SCRIPT
src="结构体指针的指针的问题 C-C++ - C语言 - CSDN社区 community_csdn_net.files/jsframework.js"
type=text/javascript></SCRIPT>
<SCRIPT type=text/javascript>/*<![CDATA[*/Include("Csdn.IM.NoticeWindow");/*]]>*/</SCRIPT>
<SCRIPT
src="结构体指针的指针的问题 C-C++ - C语言 - CSDN社区 community_csdn_net.files/clickeye.js"></SCRIPT>
<HR>
</DIV></DIV></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?