2

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

unwind
  • 391,730
  • 64
  • 469
  • 606
Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167
  • All the dots make it appear like you're using `C++` (there is no `C/C++`). I think that using `std::string` is your best bet: don't use `char *`! – pmg Mar 18 '11 at 15:30
  • What do you mean, "errorInfo is a char* I should return it"? `char*` is a pointer that may or may not be initialized, if so pointing to some memory that may or may not be a string, may or may not have enough space for the string you're createing, and has a lifetime that may or may not match what your caller expects. If you don't know anything about memory allocation in C or C++, then it's urgent that you read a book about one or both of them. There are at least 5 or 6 ways of writing a C++ function to join all this stuff together and return it, and you and your caller need to agree on one. – Steve Jessop Mar 18 '11 at 15:46
  • http://www.crasseux.com/books/ctutorial/Memory-allocation.html gives a tutorial on C memory allocation. If you've never dabbled in C before, don't worry. Memory stuff can be tricky. Just keep practicing.:) – David Weiser Mar 18 '11 at 15:50

6 Answers6

18

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 :)

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
  • Thank's I talk about C/C++ cause any solution should work for me. – Marcos Vasconcelos Mar 18 '11 at 15:34
  • 8
    You cannot talk about C/C++ because there is no such thing as C/C++. – Philipp Mar 18 '11 at 15:51
  • 1
    Just throwing oil on the fire here, but since the guy is asking for a syntax advice and that he could achieve the same results with little manipulation of his initial char array, I think you guys are being a little harsh. Also, considering C++ is a superset of C, you think it would be wrong for someone to ask: "How do I calculate the area for a Rectangle/Square". I'm pretty sure W*H does that, just like strcat can be use in a C++ context to concatenate strings. – anthonyvd Mar 18 '11 at 20:09
  • I'm going to argue that you're wrong. MANY C++ programmers intersperse C functions within their code. A class object may call strcat internally, I see this all the time. – Lisa Aug 03 '12 at 22:47
4

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 chars long.

David Weiser
  • 5,190
  • 4
  • 28
  • 35
2

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.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
1

There are many ways you can concatenate in C while using Android NDK:

Two ways I used are:

  • strcat
  • sprintf

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.

Raheel Rehman
  • 85
  • 1
  • 7
1

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

Dhanuka
  • 2,826
  • 5
  • 27
  • 38
dsmccoy
  • 81
  • 2
1

With string literals you can simple use:

char str[] = "foo" " bar";
const char *s = " 1 " " 2 ";
s = " 3 " " 4 ";
user411313
  • 3,930
  • 19
  • 16