📄 subject_22184.htm
字号:
<p>
序号:22184 发表者:xiangxing 发表日期:2002-11-23 13:51:12
<br>主题:求助:一个可能是指针的问题,诚望好心高手进来看看
<br>内容://首先说明,程序运行的结果如下;<BR>//第一次单击Button1后,Edit1中显示的是"hello",(这是正确的)<BR>//第二次单击Button1后,Edit1中显示的是"helloo",(这不是所期望的,本来是想还是显示<BR>//hello,因为都已把Edit1,C清空了,即Edit1->Clear();C=NULL;不应该受第一次单击<BR>//Button1影响)<BR>//第三次单击Button1后,Edit1中显示的是"hellooo",(每单击一次就多一个o,一直到溢出)<BR>//诚请好心高手修改程序以便每次单结Button1,A和B的值都不变且重新并结和赋值给<BR>//C,Edit1每次都显示C的内容"hello",之后还要使C中不含任何内容(当然不能把A,B定义<BR>//为常量以使其值不能改变,因为可能其它地方可能要改变值)<BR>//表达能力有限,可能说的还不明白,不过还是相信你一定明白我说什么<BR>//---------------------------------------------------------------------------<BR><BR>#include <vcl.h><BR>#pragma hdrstop<BR><BR>#include "Unit1.h"<BR>//---------------------------------------------------------------------------<BR>#pragma package(smart_init)<BR>#pragma resource "*.dfm"<BR>TForm1 *Form1;<BR>//---------------------------------------------------------------------------<BR>__fastcall TForm1::TForm1(TComponent* Owner)<BR> : TForm(Owner)<BR>{<BR>}<BR>//---------------------------------------------------------------------------<BR><BR>char* AddCharToString(char *dst, const char src)//把一字符并结到字符串的最后,并返回<BR>{<BR> char tail[2];<BR><BR> tail[0] = src;<BR> tail[1] = '\0';<BR><BR> return strcat(dst,tail);<BR>}<BR>//****************************************************************<BR><BR>void __fastcall TForm1::Button1Click(TObject *Sender)<BR>{<BR> char* A="hell";<BR> char B='o';<BR> char* C;<BR> C=AddCharToString(A,B);<BR> Edit1->Clear();<BR> Edit1->Text=C;<BR> C=NULL;<BR>}<BR>//---------------------------------------------------------------------------<BR>
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
回复者:未出手的飞刀 回复日期:2002-11-23 14:07:56
<br>内容:<BR>你这程序在 CBC 能运行?<BR><BR>char* A="hell";<BR>char B='o';<BR>char* C;<BR><BR>//C=AddCharToString(A,B);<BR>{<BR> char tail[2];<BR><BR> tail[0] = src;<BR> tail[1] = '\0';<BR><BR>// return strcat(dst,tail);<BR> C = strcat(A,tail) ; /** 在这里会出错,因为A的空间不足以容纳A + tail **/<BR>}<BR><BR>Edit1->Clear();<BR>Edit1->Text=C;<BR>C=NULL;<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:xiangxing 回复日期:2002-11-23 15:00:32
<br>内容:我用的是BCB<BR><BR>我还点搞不明白:<BR>{<BR> char tail[2];<BR><BR> tail[0] = src;<BR> tail[1] = '\0';<BR><BR>// return strcat(dst,tail);<BR> C = strcat(A,tail) ; /** 在这里会出错,因为A的空间不足以容纳A + tail **/<BR>}<BR>前不需要函数头吗?<BR>另外怎么你那程序又怎使每单击Button1一次就使A,B并结并显示一次<BR><BR>我希望通过修改<BR>char* AddCharToString(char *dst, const char src)函数和<BR>void __fastcall TForm1::Button1Click(TObject *Sender)单击事件来达到目的,可以吗?好吗?<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:xiangxing 回复日期:2002-11-23 15:02:00
<br>内容:我用的是BCB<BR><BR>我还点搞不明白:<BR>{<BR> char tail[2];<BR><BR> tail[0] = src;<BR> tail[1] = '\0';<BR><BR>// return strcat(dst,tail);<BR> C = strcat(A,tail) ; /** 在这里会出错,因为A的空间不足以容纳A + tail **/<BR>}<BR>前不需要函数头吗?<BR>另外你那程序又怎么使得每单击Button1一次就使A,B并结并显示一次<BR><BR>我希望通过修改<BR>char* AddCharToString(char *dst, const char src)函数和<BR>void __fastcall TForm1::Button1Click(TObject *Sender)单击事件来达到目的,可以吗?好吗?<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:John Lan 回复日期:2002-11-24 14:30:54
<br>内容:no, No, NO. <BR>congratulation, u have produced a classic bug. :-)<BR><BR>/*<BR>void __fastcall TForm1::Button1Click(TObject *Sender)<BR>{<BR> char* A = "hell";//u can not write to A, because A point to const data section<BR> char B = 'o';<BR> char* C;<BR> C = AddCharToString(A,B);// faint! u write 2 char to the tail of "hell" !<BR> Edit1->Clear();<BR> Edit1->Text = C;<BR> C = NULL;<BR>}<BR>*/<BR>please refer :<BR>http://www.vchelp.net/cndevforum/subject_view.asp?subject_id=21596&forum_id=<BR><BR>给你改改吧:<BR><BR>void __fastcall TForm1::Button1Click(TObject *Sender)<BR>{<BR> char A[16] = "hell"; // like this, NOT char *A ! u MUST allocate memory for modify<BR> char B = 'o';<BR> char* C; <BR> C = AddCharToString(A,B);<BR> Edit1->Clear();<BR> Edit1->Text = C;<BR> C = NULL;<BR>}<BR><BR>(u wasted my codes: new_style_strcat() :-) )<BR>2002-11-24 14:39:04
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -