可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
How do I concatenate Strings with C/C++?
I tried the following ways:
PS: errorInfo is a char * I should return it.
errorInfo = strcat("Workflow: ", strcat( workflowToString(workflow).utf8(), strcat(" ERROR: ", errorCode.utf8))); sprintf(errorInfo, "Workflow %s ERROR: %s", workflowToString(workflow).utf8(), errorCode.utf8()); errorInfo = "Workflow: " + workflowToString(workflow).utf8() + " ERROR: " + errorCode.utf8;
Just the sprintf compiles but when running my application crash.
PS: I'm using NDK from Android
回答1:
According to this page strcat
does the following:
Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a new null-character is appended at the end of the new string formed by the concatenation of both in destination.
In your implementation, however, "Workflow: "
is a constant string. You cannot modify that string, which is what strcat
would do. In order to do that, create a string like:
char message[1000]; strcpy(message, "Workflow: "); strcat(message, "other string"); ....
However, be careful about the utf8 character encoding because one utf8 code point could be multiple char
s long.
回答2:
There ISN'T such a language as C/C++. There is C, and there is C++.
- In C++ you concatenate
std::string
's by using operator+
- In C, you use strcat
I know this doesn't quite answer your question, this is just an outcry :)
回答3:
Concatenation is almost always the wrong idiom for string building, especially in C. It's error-prone, clutters your code, and has extremely bad asymptotic performance (i.e. O(n^2)
instead of O(n)
for building a string of length n
).
Instead you should use the snprintf
function, as in:
snprintf(buf, sizeof buf, "Workflow: %s ERROR: %s", workflow, error);
or if you're writing to a file/socket/etc. and don't need to keep the resulting string in memory, simply use fprintf
to begin with.
回答4:
With string literals you can simple use:
char str[] = "foo" " bar"; const char *s = " 1 " " 2 "; s = " 3 " " 4 ";
回答5:
By using strcat()
, you are working in c, not c++. c is not going to automatically manage memory for you. c can be confusing since sometimes it seems like it has a string data type when all it is doing is providing you a string interface to arrays of characters. For one thing, the first argument to strcat()
has to be writable and have enough room to add the second string.
char *out = strcat("This", "nThat");
is asking c to stomp on string literal memory.
In general, you should NEVER use strcat()/sprintf
, as in the above "chosen" answer. You can overwrite memory that way. Use strncat()/snprintf()
instead to avoid buffer overruns. If you don't know the size to pass to "n" in strncat()
, you're likely doing something wrong.
One way to do this in c would be:
#define ERROR_BUF_SIZE 2048 // or something big enough, you have to know in c char errorInfo[ERROR_BUF_SIZE]; snprintf(errorInfo, ERROR_BUF_SIZE, "Workflow %s ERROR: %s", workflowToString(workflow).utf8(), errorCode.utf8());
or similarly using strncpy/strncat
回答6:
There are many ways you can concatenate in C while using Android NDK:
Two ways I used are:
here is example:
enter code here
strcat
char* buffer1=(char*)malloc(250000); char* buffer2=(char*)malloc(250000); char* buffer3=(char*)malloc(250000); buffer1 = strcat(buffer1, buffer2);
sprintf
sprintf(buffer3,"this is buffer1: %s and this is buffer2:%s",buffer1,buffer2);`
sprintf returns length of your string
strcat is not recommended as its use more memory.. you can use sprintf or others like strcpy.
Hope it helps.