【发布时间】:2016-02-29 09:25:03
【问题描述】:
从第一个字符串中删除最后一个字符,然后将其与第二个字符串连接的方法是否可接受?
char *commandLinePath = server_files_directory;
commandLinePath[strlen(commandLinePath)-1] = 0;
char fullPath[strlen(commandLinePath) + strlen(requestPath)];
strcpy(fullPath, commandLinePath);
strcat(fullPath, requestPath);
让我们假设 server_files_directory 很好 (char *) 并且已经初始化。
我担心的是:删除部分是否正确,生成的fullPath的大小是否正确等
【问题讨论】:
-
这取决于
server_files_directory是什么。例如,修改字符串字面量是非法的。 -
char *server_files_directory;
-
取消引用具有自动存储持续时间或
NULL的未初始化变量是非法的。 -
假设 server_files_directory 没问题并且已经初始化。
-
我担心的是:删除部分是否正确,生成的fullPath的大小是否正确等
标签: c arrays string char concatenation