【发布时间】:2015-08-17 15:47:38
【问题描述】:
我正在通过使用与 C 的字符串连接来发出动态 PUT 请求。我的问题是,在第一个请求之后,我需要保持静态 putEndpoint 的字符串被我正在使用的字符串连接更改它是为了。
char putEndpoint[] = "PUT /api/v1/products/";
char http[] = " HTTP/1.1";
char productID[idLen];
for(int i = 0; i < 13; i++) {
productID[i] = newTag[i];
}
// going into this strcat, putEndpoint correctly = "PUT /api/v1/products/"
char *putRequestID = strcat(putEndpoint,productID);
// putEndpoint now = "PUT /api/v1/products/xxxxxxxxxxx"
char *putRequestEndpoint = strcat(putRequestID,http);
现在,如果我要进行第二次调用(我需要这样做),putEndpoint 将初始化为 "PUT /api/v1/products/xxxxxxxxxxx"。
编辑:有没有替代
strcat()可以完成这种连接? 我现在明白strcat()是为了改变价值观。
【问题讨论】:
-
你那里有一些讨厌的错误......
-
@PaulR 需要详细说明吗?
-
阅读
strcat它将第二个字符串连接到第一个字符串中。结果 - 字符串 1 被修改。 -
strcat不分配新字符串。它只是将数据附加到第一个参数。那就是它多次更改putEndpoint。 -
由于要连接的所有 3 个字符串都有最大大小,因此形成一个大约为这些大小总和的目标数组。
标签: c string-concatenation strcat