《剑指Offer》有一个主题,“要一个字符数组空格弦“%20”顶替“,阅读这本书的想法,然后,我写了一个程序,当输入空气格当太多,一个错误“memory clobbered before allocated block”。其原因尚不清楚栈存储器,存的分配和差别。错误代码例如以下:
#include <stdio.h>
#include <malloc.h>
#include <string.h>
void replaceSpace(char string[]);
int main()
{
char string[] = "This is a test!"; //字符数组。在栈内存区分配内存
replaceSpace(string);
printf("%s",string);
free(string);
return 0;
}
void replaceSpace(char string[])
{
int index, spaceNum;
size_t OriginLen, FinalLen;
int OriginIndex, FinalIndex;
index = 0;
spaceNum = 0;
OriginLen = 0;
while(string[index] != '