我当前的代码如下所示(看起来不错,但无法编译)。
char FileConfPath[256];
char *pos;
GetModuleFileNameA(0, FileConfPath, 256);
pos= strrchr(FileConfPath, '\\');
if ( pos )
strcat(FileConfPath[1], "file.conf");
else
strcat(FileConfPath, "file.conf");
生成 2 个编译器错误。
error #2140: Type error in argument 1 to 'strcat'; expected 'char * restrict' but found 'char'.
我必须这样做吗
strcat(&FileConfPath[1], "file.conf");
在这里使用地址看起来不对。
似乎错误仅针对 FileConfPath[1]
而不是针对没有索引说明符的错误。
最佳答案
这是在初始字符后放置 "file.conf"
的正确方法:
strcat(&FileConfPath[1], "file.conf");
FileConfPath[1]
是索引一的字符; &FileConfPath[1]
是索引 1 处字符的地址,这是您要传递给 strcat
的内容。
注意,如果要在斜线后放置"file.conf"
,要使用斜线后字符的地址,即
strcpy(pos+1, "file.conf");
不言而喻,pos+11
(一个用于斜杠,一个用于空终止符,九个用于“file.conf”
的字符)需要小于或等于 &FileConfPath[255]
以避免缓冲区溢出。
关于C 如何将 strcat 与静态偏移量字符数组一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23258081/