问题描述
我想通过用''\0''替换最后一个字符来缩短字符串。
以下代码显示字符串。它工作正常。这是在一个
循环中,不同的字符串显示没有问题。
------------------- ----------------------------------------
temp = strlen(aGroup);
for(i = 0; i< temp; i ++)
printf("%d%d%c\ n,i + 1,temp,aGroup [i]);
-------------------------------- -------------------------
用''\ 0'替换最后一个字符,我已经尝试了
aGroup [i-1] =''\ 0'';
或
temp--;
aGroup [temp] =''\ 0'';
或
aGroup [ temp-1] =''\''';
虽然没有编译错误,程序在运行时崩溃了
(我被问到是否向Microsoft发送错误报告。同样的错误
这三个。)
但是aGroup [22] =''\'''; "工作(我知道长度大于22)。
给出了什么?这是在VC ++ 6.0上。
验证aGroup是否已正确声明,并且它是足够大到足以存储字符串的b $ b正在复制它。
也许你正在溢出一个缓冲区,或忘了正确调用
malloc?
-Jason
-Jason
验证aGroup是否已正确声明,并且它足够大,足以存储您要复制到其中的字符串。
也许你是在满溢缓冲区,或忘了正确调用了malloc?
-Jason
-Jason
感谢您的回复。
正如我所说。该程序工作正常,直到我添加代码(三个中的任何一个
)来替换最后一个字符。我认为这应该排除你提到的潜在问题。
BTW,aGroup被声明为char aGroup [2048];这可能比它的价格大10倍。
你能发贴实际的代码吗?没有进一步的信息我很害怕
我无法帮助你。
-Jason
I want to shorten a string by replacing the last character by ''\0''.
The following code displays the string. It works fine. It''s in a
loop and different strings are displayed without problems.
-----------------------------------------------------------
temp= strlen(aGroup);
for (i=0; i< temp;i++)
printf(" %d %d %c\n", i+1,temp,aGroup[i]);
---------------------------------------------------------
To replace the last character by ''\0'', I have tried
aGroup[i-1]=''\0'';
or
temp--;
aGroup[temp]=''\0'';
or
aGroup[temp-1]=''\0'';
Though there is no compiling error, the program crashed when ran
(I was asked whether to send error report to Microsoft. Same error
for all three.)
But "aGroup[22]=''\0'';" works (I knew the length is greater than 22.)
What gives? This is on VC++ 6.0.
Verify that aGroup has been properly declared, and that it is
suffiently large enough to store the string you are copying into it.
Perhaps you are overflowing a buffer, or forgot to properly call
malloc?
-Jason
-Jason
Verify that aGroup has been properly declared, and that it is
suffiently large enough to store the string you are copying into it.
Perhaps you are overflowing a buffer, or forgot to properly call
malloc?
-Jason
-Jason
Thanks for the reply.
As I said. The program worked fine until I added the code (any one
of the three) to replace the last character. I think this should
rule out the potential problems you mentioned.
BTW, aGroup is declared as "char aGroup[2048];" which is probably
10 times larger than what it can be.
Could you post the actual code? Without further information I''m afraid
I won''t be able to assist you.
-Jason
这篇关于将字符串长度缩短1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!