我对 char * 返回值的函数有疑问
我的职能是
char * GetUnreadMessageIndexes(char * input)
{
char index [10];
int i=0;
while(*input != ':')
{
input++;
}
input++;
while(*input != ',')
{
if(*input != ' ')
{
index[i]= *input;
i++;
}
input++;
}
return index;
};
我的主要测试代码是
char * b = "+CMGL:26867689, \"REC READ\",\"+81923733737\", \n test again ";
char * a = GetUnreadMessageIndexes(b);
while(a != ((int)a + (int) strlen(a)))
{
printf("%c",*a);
a++;
}
这个函数假设解析+CMGL:之后和之前的数字字符串, 我只得到第一个数字“2”,其他值是意外值并且不正确
最佳答案
您正在传回指向堆栈中存在的变量(“索引”)的指针。一旦您离开该函数,堆栈就会展开,您的指针将指向已被堆栈中放置的任何新数据覆盖的内容。
关于c - char * 返回值的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21295951/