今天还在做着小作业,终于把框架大体搭好,准备脱离stl用c语言来解析字符串。却碰到了诸多问题。
原来堆栈基本不太懂,现在稍稍知道了,还有字符串指针和数组的区别。哎
简单的写了几个定义和初始化字符串的方式
char *szName1 = " dir \ad\ what are you doing"; // q全局const区域
char szName2[] = " dir \ad\ what are you doing";//栈 区
char *szName3 = new char[64]; //堆区
szName3 = " dir \ad\what are you doing";
char *szName4 = szName2; //栈区的指针指向栈区的内存
debug一看,原来果真只有 szName[2]是char[]型
debug信息:
#include "stdio.h"
#include "string.h"
const int MAX_LENGTH = 256;
void StrReStrEx(char **pnewstr, char c)
{
size_t i = 0;
for (; i<strlen(*pnewstr); i++)
{
if ((*pnewstr)[i] != c)
{
break;
}
}
*pnewstr = *pnewstr+i;
}
void StrReStrEx1(char *&newstr, char c)
{
size_t i = 0;
for (; i<strlen(newstr); i++)
{
if ((newstr)[i] != c)
{
break;
}
}
newstr = newstr+i;
}
int main()
{
char *szName = " dir \ad\What are you doing"; // q全局const区域
char szName2[] = " dir \ad\What are you doing";//栈 区
char *szName3 = new char[MAX_LENGTH]; //堆区
szName3 = " dir \ad\What are you doing";
char *szName4 = szName2; //栈区的指针指向栈区的内存
printf("%s\nszName:%d\n",szName, strlen(szName));
char *temp = szName;
StrReStrEx1(szName, ' ');
printf("%s\nszName1:%d\n",szName, strlen(szName));
StrReStrEx1(szName3, ' ');
printf("%s\nszName3:%d\n",szName3, strlen(szName3));
StrReStrEx1(szName4, ' ');
printf("%s\nszName4:%d\n",szName4, strlen(szName4));
// StrReStrEx1(szName2, ' ');
// printf("%s\nszName:%d\n",szName2, strlen(szName2));
return 0;
}