【发布时间】:2011-08-28 13:32:24
【问题描述】:
我正在编写一个代码来用它的值替换所有 MACROS。 如果我的宏 MAX 的值为 1000, 并且在代码中,它必须被替换为 1000。(我假设如果 MACROS 是一行中的第一个单词,那么在该行中我们不会替换 MACROS,这种情况下我们会以不同的方式处理。
//Code to replace MACROS BY THEIR VALUES
//line contains the actual one line of the code.
//line is initialized to contain as maximum number of charectos(say 100).
//SrcStr is the macro and destStr is its value.
//This block will be looped for all lines.
char* p;
p = strstr(line,srcStr);
if(p != NULL) //if the srcString is found
{
if(strlen(p) != strlen(line)) //special case
{
if( isalnum(*(p-1)) == 0 && isalnum( *(p+strlen(srcStr)))==0 )
// if the next char and prev char to our macro is not a alphabets or digits
{
/*As answered by medo42 (below)*/
memmove(p+strlen(destStr), p+strlen(srcStr),strlen(p+strlen(srcStr)+1);
memcpy(p,destStr,strlen(destStr));
}
}
else
{/* handle differently*/}
}
由于我是第一次使用memmove和memcopy,所以我怀疑上面的代码是否稳定并且可以正常工作。
上面的代码正确吗? 上面的代码对所有输入情况都稳定吗?
【问题讨论】:
-
好的,这确实是一种可怕且有限的方法。了解语法树。