I am making a dynamic PUT request through the use of string concatenation with C. My problem is that after the first request, a string that I need to remain static putEndpoint
, is altered with the string concatenation I'm using it for.
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);
Now if I were to make a 2nd call (which I will need to do), putEndpoint
initializes as "PUT /api/v1/products/xxxxxxxxxxx"
.
EDIT: Is there an alternative to
strcat()
that could accomplish this concatenation? I now understand thatstrcat()
is meant to alter values.
You can use sprintf
.
#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;
}
I ended up changing putEndpoint
to a constant and created a buffer array and that I then copied putEndpoint
into. This array then resets after each request.
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);
具有固定大小和适当范围检查的memcpy
和静态数组是您的朋友,我的朋友。
You would have to reset it with code. If you change putEndpoint[]
to a const, which will make your first line look like
const char putEndpoint[] = "PUT /api/v1/products/";
the compiler will give you an error the first time you strcat(putEndpoint,...)
because strcat will be trying to write to the constant variable. This will force you to find an alternative solution.
The code below cleanly allocates temporary memory for the endpoint string as needed, copies the first string into it, concatenates the next two onto it, uses it, and finally de-allocates it and sets the pointer back to 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;
Before answering this, I refreshed my memory of C string manipulation using this WIKIBOOKS site . I'd recommend it for further reading on this subject.
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.