我正在编写一个代码来用它的值替换所有 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
and memcopy
,我怀疑上面的代码是否稳定并且可以正常工作。
上面的代码正确吗?上面的代码对于所有输入情况都稳定吗?