简体   繁体   中英

C how to use strcat with static offset char array

My current code looks like this (looks good on the eye but doesn't compile).

char FileConfPath[256];
char *pos;

GetModuleFileNameA(0, FileConfPath, 256);
pos= strrchr(FileConfPath, '\\');
if ( pos )
  strcat(FileConfPath[1], "file.conf");
else
  strcat(FileConfPath, "file.conf");

Generates 2 compiler errors.

error #2140: Type error in argument 1 to 'strcat'; expected 'char * restrict' but found 'char'.

Must I do

strcat(&FileConfPath[1], "file.conf");

doesn't look right to use addresses here.

Seems the error is for FileConfPath[1] only not for the one without the index specifier.

This is the correct way of placing "file.conf" after the initial character:

strcat(&FileConfPath[1], "file.conf");

FileConfPath[1] is the character at index one; &FileConfPath[1] is the address of the character at index one, which is what you want to pass to strcat .

Note that if you want to place "file.conf" after the slash, you want to use the address of the character after the slash, ie

strcpy(pos+1, "file.conf");

It goes without saying that pos+11 (one for the slash plus one for null terminator plus nine for the characters of "file.conf" ) needs to be less than or equal to &FileConfPath[255] to avoid buffer overrun.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM