Lil*_*ter 9 c malloc pointers character-arrays
我有一段代码,我试图在括号内抓取一个表达式,然后使用它.在下面的代码开始的时候,我正在迭代字符数组,并且pcc是指向当前字符的指针,该字符已被确定为a '('.我的目标是将paranthetical表达式放在一个字符数组中pe.
int nnrp = 1; /* Net number of right parantheses */
char * pbpe = pcc; /* Pointer to the beginning paranthetical expression */
for (++pcc; *pcc!= '\0' && nnrp != 0; ++pcc)
{
if (*pcc == '(')
{
++nnrp;
}
else if (*pcc == ')')
{
--nnrp;
}
else if (*pcc == '\0')
{
sprintf(err, "Unbalanced paranthesis");
return -1;
}
}
/* If we're here, *pcc is the closing paranathesis of *pbpe */
long nel = pcc - pbpe; /* New expression length */
if (nel == 1)
{
sprintf(err, "Empty parenthesis");
return -1;
}
char * pe = (char*)malloc(nel+1); /* Paranthetical expression */
strncpy(pcc+1, pcc, nel);
pe[nel] = '\0';
Run Code Online (Sandbox Code Playgroud)
但我的IDE(XCode 6.0)正在给我警告
"语义问题:隐式声明库函数'malloc',类型为'void*(unsigned long)'"
就strncpy(pcc+1, pcc, nel);行了.我在想
提前致谢.
Dav*_*son 28
尝试将此行添加到文件的顶部:
#include <stdlib.h>
Run Code Online (Sandbox Code Playgroud)
这将带来明确的声明malloc,所以你不应该得到那个警告.
您可能会收到警告,因为您忘记在文件中包含stdlib.h.编译器对你很好,并给你一个隐式声明,malloc以便代码编译.一般来说,最好包含显式声明,以便编译器真正知道您要调用的函数类型,并且修复所有可能的警告也很好,这样您的构建过程将是干净的,您可以注意到更重要的警告.所以是的,你应该解决它.