结构体指针的指针的问题 c-c++ - c语言 - csdn社区 community_csdn_net.htm

来自「最全的介绍C语言结构体的使用方法和使用技巧!」· HTM 代码 · 共 348 行 · 第 1/2 页

HTM
348
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0048)http://topic.csdn.net/t/20060731/21/4917799.html -->
<HTML lang=zh-CN xml:lang="zh-CN" 
xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>结构体指针的指针的问题 C/C++ / C语言 - CSDN社区 community.csdn.net</TITLE>
<META content=all name=robots>
<META http-equiv=Content-Language content=zh-CN>
<META http-equiv=Content-Type content="text/html; charset=utf-8"><LINK 
href="结构体指针的指针的问题 C-C++ - C语言 - CSDN社区 community_csdn_net.files/topic.css" 
type=text/css rel=stylesheet>
<SCRIPT src="结构体指针的指针的问题 C-C++ - C语言 - CSDN社区 community_csdn_net.files/Topic.js" 
type=text/javascript></SCRIPT>

<META content=" C/C++ C语言" name=Keywords>
<META 
content='#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,' 
name=description>
<META content="MSHTML 6.00.2800.1106" name=GENERATOR></HEAD>
<BODY>
<DIV id=wrapper>
<DIV id=innerWrapper>
<TABLE class=CSDN-PHF id=CSDNPHL cellSpacing=1 cellPadding=0 border=0>
  <TBODY>
  <TR>
    <TD><A href="http://www.csdn.net/" rel=external>CSDN首页</A></TD>
    <TD><A href="http://news.csdn.net/" rel=external>新闻</A></TD>
    <TD><A href="http://bbs.csdn.net/" rel=external><STRONG>论坛</STRONG></A></TD>
    <TD><A href="http://groups.csdn.net/" 
    rel=external><STRONG>小组</STRONG></A></TD>
    <TD><A href="http://blog.csdn.net/" 
    rel=external><STRONG>Blog</STRONG></A></TD>
    <TD><A href="http://dev.csdn.net/" rel=external>文档</A></TD>
    <TD><A href="http://download.csdn.net/" rel=external>下载</A></TD>
    <TD><A href="http://book.csdn.net/" rel=external>读书</A></TD>
    <TD><A href="http://tag.csdn.net/" 
rel=external><STRONG>Tag</STRONG></A></TD>
    <TD><A href="http://wz.csdn.net/" rel=external><STRONG>网摘</STRONG></A></TD>
    <TD><A href="http://search.csdn.net/" 
    rel=external><STRONG>搜索</STRONG></A></TD>
    <TD><A href="http://dotnet.csdn.net/" 
      rel=external><STRONG>.NET</STRONG></A></TD>
    <TD><A href="http://java.csdn.net/" 
    rel=external><STRONG>Java</STRONG></A></TD>
    <TD><A href="http://gamedev.csdn.net/" rel=external>游戏</A></TD>
    <TD><A href="http://live.csdn.net/" rel=external>视频</A></TD>
    <TD><A href="http://job.csdn.net/" rel=external>人才</A></TD>
    <TD><A href="http://prj.csdn.net/" rel=external>外包</A></TD>
    <TD><A href="http://www.dearbook.com.cn/" rel=external>第二书店</A></TD>
    <TD><A href="http://www.programmer.com.cn/" 
  rel=external>程序员</A></TD></TR></TBODY></TABLE>
<DIV id=logo><!--

<table border="0" cellspacing="0" cellpadding="0" width="770px"><tr><td style="text-align:left;"><a
href="http://community.csdn.net/" rel="external"><img class="logo" src="http://www.csdn.net/ui/images/logo_csdn.gif" alt="CSDN社区" /></a></td><td style="text-align:center"><a
href="http://g.csdn.net/5022240" rel="external"><img class="logo" src="http://topic.csdn.net/images/adv/468-60.gif" alt="" style="margin:0" /></a></td><td style="text-align:right"><a
href="http://g.csdn.net/5021657" rel="external"><img src="http://topic.csdn.net/images/adv/120_60.gif" alt="" style="margin:0"/></a></td>
</tr><tr id="bigFlash" style="text-align:center"><td colspan="3"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="760" height="300">
  <param name="movie" value="http://topic.csdn.net/images/adv/760-300.swf" />
  <param name="quality" value="high" />
  <embed src="http://topic.csdn.net/images/adv/760-300.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="760" height="300"></embed>
</object></td></tr>
</table>
<script type="text/javascript">setTimeout("document.getElementById('bigFlash').style.display='none'", 5000);</script>
--><A href="http://community.csdn.net/" rel=external><IMG class=logo alt=CSDN社区 
src="结构体指针的指针的问题 C-C++ - C语言 - CSDN社区 community_csdn_net.files/logo_csdn.gif"></A><A 
href="http://g.csdn.net/5022240" rel=external><IMG alt="" 
src="结构体指针的指针的问题 C-C++ - C语言 - CSDN社区 community_csdn_net.files/468-60.gif"></A><A 
href="http://g.csdn.net/5021657" rel=external><IMG alt="" 
src="结构体指针的指针的问题 C-C++ - C语言 - CSDN社区 community_csdn_net.files/120_60.gif"></A> 
<IMG style="DISPLAY: none" alt="" 
src="结构体指针的指针的问题 C-C++ - C语言 - CSDN社区 community_csdn_net.files/pv.aspx"> </DIV>
<DIV id=navigation>
<DIV id=search>
<DIV id=innerSearch><INPUT id=key onfocus="value=''"><A 
onclick="return Search()" 
href="http://topic.csdn.net/t/20060731/21/4917799.html#">搜索</A>&nbsp;|&nbsp;<A 
title=功能强大的网络收藏夹,一秒钟操作就可以轻松实现保存带来的价值、分享带来的快乐 
href="javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(saveit=window.open('http://wz.csdn.net/storeit.aspx?t='+escape(d.title)+'&amp;u='+escape(d.location.href)+'&amp;c='+escape(t),'saveit','scrollbars=no,width=590,height=300,left=75,top=20,status=no,resizable=yes'));saveit.focus();">收藏</A>&nbsp;|&nbsp;<A 
onclick="return Print()" 
href="http://topic.csdn.net/t/20060731/21/4917799.html#">打印</A>&nbsp;|&nbsp;<A 
onclick="return Close()" 
href="http://topic.csdn.net/t/20060731/21/4917799.html#">关闭</A> </DIV></DIV>
<DIV><A class=CSDNRoom href="http://community.csdn.net/" 
rel=external>CSDN社区</A>&nbsp;&gt;&nbsp; <A 
href="http://community.csdn.net/Expert/ForumsList.asp?typenum=1&amp;roomid=56" 
rel=external>C/C++</A>&nbsp;&gt;&nbsp; <A 
href="http://community.csdn.net/Expert/ForumList.asp?typenum=1&amp;roomid=5601" 
rel=external>C语言</A> </DIV></DIV>
<DIV id=mainWrapper>
<DIV id=pageContent>
<DIV id=innerContent>
<H1>结构体指针的指针的问题</H1>
<DIV class=author><A class=anchor name=Top>楼主</A>xiphias(var)<INPUT class=user1 title="一级用户 该版得分小于等于100分" type=button>2006-07-31 
21:53:57 在 C/C++ / C语言 提问</DIV>
<P>#include &nbsp; &lt;stdio.h&gt; &nbsp; <BR>&nbsp; #include &nbsp; 
&lt;stdlib.h&gt; &nbsp; <BR>&nbsp; #include &nbsp; &lt;assert.h&gt; &nbsp; 
<BR>&nbsp; #include &nbsp; &lt;string.h&gt; &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; 
typedef &nbsp; struct &nbsp; tagTItem &nbsp; <BR>&nbsp; { &nbsp; <BR>&nbsp; 
&nbsp; &nbsp; char &nbsp; &nbsp; Name[10]; &nbsp; <BR>&nbsp; &nbsp; &nbsp; 
double &nbsp; Amt; &nbsp; <BR>&nbsp; } &nbsp; TItem; &nbsp; <BR>&nbsp; &nbsp; 
<BR>&nbsp; void &nbsp; getmem(TItem** &nbsp; item) &nbsp; <BR>&nbsp; { &nbsp; 
<BR>&nbsp; &nbsp; &nbsp; int &nbsp; i=0; &nbsp; <BR>&nbsp; &nbsp; &nbsp; item 
&nbsp; = &nbsp; (TItem**)malloc(2*sizeof(TItem*)); &nbsp; <BR>&nbsp; &nbsp; 
&nbsp; if &nbsp; (item==NULL) &nbsp; { &nbsp; printf("malloc &nbsp; e.\n"); 
&nbsp; return; &nbsp; } &nbsp; <BR>&nbsp; &nbsp; &nbsp; for &nbsp; (i=0; &nbsp; 
i&lt;2; &nbsp; i++) &nbsp; { &nbsp; <BR>&nbsp; &nbsp; &nbsp; &nbsp; *(item+i) 
&nbsp; = &nbsp; (TItem*)malloc(sizeof(TItem)); &nbsp; <BR>&nbsp; &nbsp; &nbsp; 
&nbsp; sprintf((*(item+i))-&gt;Name, &nbsp; "china%d", &nbsp; i+1); &nbsp; 
<BR>&nbsp; &nbsp; &nbsp; &nbsp; (*(item+i))-&gt;Amt &nbsp; = &nbsp; (i+1)*12.0; 
&nbsp; <BR>&nbsp; &nbsp; &nbsp; &nbsp; sprintf((*(item+i))-&gt;Name, &nbsp; 
"china%d", &nbsp; i+1); &nbsp; <BR>&nbsp; &nbsp; &nbsp; &nbsp; 
(*(item+i))-&gt;Amt &nbsp; = &nbsp; (i+1)*12.0; &nbsp; <BR>&nbsp; &nbsp; &nbsp; 
} &nbsp; <BR>&nbsp; } &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; int &nbsp; main(int 
&nbsp; argc, &nbsp; char* &nbsp; argv[]) &nbsp; <BR>&nbsp; { &nbsp; <BR>&nbsp; 
&nbsp; &nbsp; int &nbsp; i &nbsp; = &nbsp; 0, &nbsp; n &nbsp; = &nbsp; 0; &nbsp; 
<BR>&nbsp; &nbsp; &nbsp; TItem &nbsp; **itm &nbsp; = &nbsp; NULL; &nbsp; &nbsp; 
&nbsp; <BR>&nbsp; &nbsp; &nbsp; getmem(itm); &nbsp; <BR>&nbsp; &nbsp; &nbsp; 
for(i=0; &nbsp; i&lt;2; &nbsp; i++) &nbsp; { &nbsp; <BR>&nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; printf("[%s]\n", &nbsp; (*itm++)-&gt;Name); &nbsp; <BR>&nbsp; 
&nbsp; &nbsp; } &nbsp; <BR>&nbsp; &nbsp; &nbsp; free(itm); &nbsp; <BR>&nbsp; 
&nbsp; &nbsp; getchar(); &nbsp; <BR>&nbsp; &nbsp; &nbsp; return &nbsp; 0; &nbsp; 
<BR>&nbsp; } &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; // &nbsp; 
不知道哪里错了,打印的时候就core &nbsp; dump勒,帮忙看看,指针不会用啊 <SPAN>问题点数:20、回复次数:6</SPAN><A 
href="http://topic.csdn.net/t/20060731/21/4917799.html#">Top</A> </P>
<DIV class=tagad><IFRAME 
src="结构体指针的指针的问题 C-C++ - C语言 - CSDN社区 community_csdn_net.files/Board.htm" 
frameBorder=0 scrolling=no></IFRAME></DIV>
<H3><STRONG><A class=anchor name=r_35893378>1 楼</A>ddstudent()<INPUT class=user2 title="二级用户 该版得分小于等于500分,大于100分" type=button></STRONG><SPAN>回复于 
2006-07-31 22:11:53 得分 <EM>5</EM></SPAN></H3>
<P>#include &nbsp; &lt;stdio.h&gt; &nbsp; <BR>&nbsp; #include &nbsp; 
&lt;stdlib.h&gt; &nbsp; <BR>&nbsp; #include &nbsp; &lt;assert.h&gt; &nbsp; 
<BR>&nbsp; #include &nbsp; &lt;string.h&gt; &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; 
typedef &nbsp; struct &nbsp; tagTItem &nbsp; <BR>&nbsp; { &nbsp; <BR>&nbsp; 
&nbsp; &nbsp; char &nbsp; &nbsp; Name[10]; &nbsp; <BR>&nbsp; &nbsp; &nbsp; 
double &nbsp; Amt; &nbsp; <BR>&nbsp; } &nbsp; TItem; &nbsp; <BR>&nbsp; &nbsp; 
<BR>&nbsp; void &nbsp; getmem(TItem** &nbsp; item) &nbsp; <BR>&nbsp; { &nbsp; 
<BR>&nbsp; &nbsp; &nbsp; int &nbsp; i=0; &nbsp; <BR>&nbsp; &nbsp; &nbsp; for 
&nbsp; (i=0; &nbsp; i&lt;2; &nbsp; i++) &nbsp; &nbsp; <BR>&nbsp; &nbsp; &nbsp; { 
&nbsp; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *(item+i) &nbsp; = &nbsp; 
(TItem*)malloc(sizeof(TItem)); &nbsp; <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
sprintf((*(item+i))-&gt;Name, &nbsp; "china%d", &nbsp; i+1); &nbsp; <BR>&nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; (*(item+i))-&gt;Amt &nbsp; = &nbsp; (i+1)*12.0; 
&nbsp; <BR>&nbsp; // &nbsp; &nbsp; &nbsp; &nbsp; sprintf((*(item+i))-&gt;Name, 
&nbsp; "china%d", &nbsp; i+1); &nbsp; &nbsp; &nbsp; // &nbsp; 重复 &nbsp; 
<BR>&nbsp; // &nbsp; &nbsp; &nbsp; &nbsp; (*(item+i))-&gt;Amt &nbsp; = &nbsp; 
(i+1)*12.0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // &nbsp; 重复 &nbsp; <BR>&nbsp; 
&nbsp; &nbsp; } &nbsp; <BR>&nbsp; } &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; int 
&nbsp; main(int &nbsp; argc, &nbsp; char* &nbsp; argv[]) &nbsp; <BR>&nbsp; { 
&nbsp; <BR>&nbsp; &nbsp; &nbsp; int &nbsp; i &nbsp; = &nbsp; 0, &nbsp; n &nbsp; 
= &nbsp; 0; &nbsp; <BR>&nbsp; &nbsp; &nbsp; TItem &nbsp; **itm &nbsp; = &nbsp; 
NULL; &nbsp; &nbsp; &nbsp; <BR>&nbsp; &nbsp; &nbsp; // &nbsp; 程序最主要错在把 &nbsp; 
itm &nbsp; = &nbsp; malloc &nbsp; 放在函数里, &nbsp; 函数结束返回时並不会传回來 &nbsp; <BR>&nbsp; 
&nbsp; &nbsp; // &nbsp; itm &nbsp; 还是 &nbsp; NULL, &nbsp; 如果要在函数里改 &nbsp; itm 
&nbsp; 的值, &nbsp; 要传 &nbsp; &amp;itm &nbsp; <BR>&nbsp; &nbsp; &nbsp; itm &nbsp; 
= &nbsp; (TItem**)malloc(2*sizeof(TItem*)); &nbsp; &nbsp; <BR>&nbsp; &nbsp; 
&nbsp; if &nbsp; (itm==NULL) &nbsp; &nbsp; <BR>&nbsp; &nbsp; &nbsp; { &nbsp; 
&nbsp; <BR>&nbsp; &nbsp; &nbsp; printf("malloc &nbsp; e.\n"); &nbsp; &nbsp; 
<BR>&nbsp; &nbsp; &nbsp; return &nbsp; 0; &nbsp; &nbsp; <BR>&nbsp; &nbsp; &nbsp; 
} &nbsp; <BR>&nbsp; &nbsp; &nbsp; getmem(itm); &nbsp; <BR>&nbsp; &nbsp; &nbsp; 
for(i=0; &nbsp; i&lt;2; &nbsp; i++) &nbsp; { &nbsp; <BR>&nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; printf("[%s]\n", &nbsp; (*(itm+i))-&gt;Name); &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // &nbsp; itm+i &nbsp; <BR>&nbsp; &nbsp; 
&nbsp; } &nbsp; <BR>&nbsp; &nbsp; &nbsp; free(itm); &nbsp; <BR>&nbsp; &nbsp; 
&nbsp; getchar(); &nbsp; <BR>&nbsp; &nbsp; &nbsp; return &nbsp; 0; &nbsp; 
<BR>&nbsp; } &nbsp; <BR>&nbsp; <A 
href="http://topic.csdn.net/t/20060731/21/4917799.html#">Top</A></P>
<H3><STRONG><A class=anchor name=r_35893430>2 楼</A>jixingzhong(瞌睡虫·星辰)<INPUT class=star4 title="四星用户 该版得分小于等于100000分,大于50000分" type=button></STRONG><SPAN>回复于 
2006-07-31 22:17:13 得分 <EM>10</EM></SPAN></H3>
<P>void &nbsp; getmem(TItem** &nbsp; item) &nbsp; <BR>&nbsp; { &nbsp; <BR>&nbsp; 
&nbsp; &nbsp; int &nbsp; i=0; &nbsp; <BR>&nbsp; &nbsp; &nbsp; *item &nbsp; = 
&nbsp; (TItem*)malloc(2*sizeof(TItem)); &nbsp; <BR>&nbsp; &nbsp; &nbsp; if 
&nbsp; (item==NULL) &nbsp; { &nbsp; printf("malloc &nbsp; e.\n"); &nbsp; return; 
&nbsp; } &nbsp; <BR>&nbsp; &nbsp; &nbsp; for &nbsp; (i=0; &nbsp; i&lt;2; &nbsp; 
i++) &nbsp; &nbsp; <BR>&nbsp; &nbsp; &nbsp; { &nbsp; <BR>&nbsp; &nbsp; &nbsp; 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?