J
John Smith
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);
---------------------------------------------------------
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.
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);
---------------------------------------------------------
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.