【问题标题】:String concatenation without altering original values - C字符串连接而不改变原始值 - C
【发布时间】: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


【解决方案1】:

您可以使用sprintf

A simple working example-

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

 int main(void) {

      char putEndpoint[] = "PUT /api/v1/products/";
      char http[] = " HTTP/1.1";
      char *putRequestEndpoint;

      putRequestEndpoint=malloc(strlen(putEndpoint)+strlen(http)+1); //allocating memory

      sprintf(putRequestEndpoint,"%s%s",putEndpoint,http); // formatted output is stored in putRequestEndpoint
      printf("%s\n",putRequestEndpoint);
      printf("%s",putEndpoint); // will print original string unchanged.
      free(putRequestEndpoint);  //freeing memory
      return 0;
   }

【讨论】:

    【解决方案2】:

    我最终将putEndpoint 更改为常量并创建了一个缓冲区数组,然后我将putEndpoint 复制到其中。然后这个数组在每次请求后重置。

    const char putEndpoint[] = "PUT /api/v1/products/";
    char http[] = " HTTP/1.1";
    char productID[idLen];
    
    char putRequestBuffer[100];
    strcpy(putRequestBuffer, putEndpoint);
    strcat(putRequestBuffer, productID);
    
    char *putRequestEndpoint = strcat(putRequestBuffer,http);
    

    【讨论】:

    • 代码如何知道char putRequestBuffer[100] 足够大?最好使用 something,例如 char putRequestBuffer[sizeof putEndpoint + sizeof http + sizeof productID];
    【解决方案3】:

    memcpy 和具有固定大小和适当边界检查的静态数组是你的朋友,我的朋友。

    【讨论】:

      【解决方案4】:

      您必须使用代码重置它。如果您将putEndpoint[] 更改为 const,这将使您的第一行看起来像

      const char putEndpoint[] = "PUT /api/v1/products/";
      

      当你第一次strcat(putEndpoint,...) 时,编译器会给你一个错误,因为 strcat 将尝试写入常量变量。这将迫使您找到替代解决方案。

      下面的代码根据需要为端点字符串干净地分配临时内存,将第一个字符串复制到其中,将接下来的两个字符串连接到它上面,使用它,最后取消分配并将指针设置回 NULL。

      int lengthEndpoint = 0;
      char* putRequestEndpoint = NULL;
      
      lengthEndpoint = strlen(putEndpoint) + strlen(productID) + strlen(http);
      lengthEndpoint += 1; // add room for null terminator
      
      putRequestEndpoint = malloc(lengthEndpoint);
      strcpy(putRequestEndpoint, putEndpoint);
      strcat(putRequestEndpoint, productID);
      strcat(putRequestEndpoint, http);
      
      // do something with putRequestEndpoint
      
      free(putRequestEndpoint);
      putRequestEndpoint = NULL;
      

      在回答这个问题之前,我使用这个WIKIBOOKS site 刷新了我对 C 字符串操作的记忆。我推荐它进一步阅读这个主题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-06-17
        • 1970-01-01
        • 1970-01-01
        • 2021-04-01
        • 2015-01-12
        • 1970-01-01
        • 2019-10-19
        相关资源
        最近更新 更多