One of the answers to Why do you not use C for your web apps? contains the following:
For the C crap example below:
const char* foo = "foo"; const char* bar = "bar"; char* foobar = (char*)malloc(strlen(foo)+strlen(bar)+1); strcpy(foobar, foo); strcat(foobar, foo);
Actually, constants CAN AND SHOULD be concatenated naturally in C:
const char foo[] = "foo"; const char bar[] = "bar"; char foobar[] = foo bar; // look Ma, I did it without any operator!
And using [] instead of * will even let you modify the string, or find their length:
int foo_str_len = sizeof(foobar)-1;
So, PLEASE, before you (falsely) claim that C is difficult to use with strings, learn how to use C.
I've tried it myself but get an error:
expected ‘,’ or ‘;’ before string constant
So my question is: Do I need to tell the compiler something in order to make this work or is the above post simply wrong? Please note that I'm aware of other ways to concatenate character arrays in C.
In C, the strcat() function is used to concatenate two strings. It concatenates one string (the source) to the end of another string (the destination). The pointer of the source string is appended to the end of the destination string, thus concatenating both strings.
Concatenation is the process of combining two or more strings to form a new string by subsequently appending the next string to the end of the previous strings. In Java, two strings can be concatenated by using the + or += operator, or through the concat() method, defined in the java. lang. String class.
The strcat() function operates on null-ended strings. The string arguments to the function should contain a null character (\0) that marks the end of the string. No length checking is performed. You should not use a literal string for a string1 value, although string2 may be a literal string.
The concatenation of strings is a process of combining two strings to form a single string. If there are two strings, then the second string is added at the end of the first string. For example, Hello + javaTpoint = Hello javaTpoint.
(char*)malloc
Never typecast the result of malloc in C. Read this and this.
Actually, constants CAN AND SHOULD be concatenated naturally in C
No, string literals can and should be concatenated in C. "foo"
is a string literal and const char foo[]
is a constant string (array of characters). The code "foo" "bar"
will concatenate automatically, the code foo bar
will not.
If you want, you can hide the string literals behind macros:
#define foo "foo" #define bar "bar" char foobar[] = foo bar; // actually works
So, PLEASE, before you (falsely) claim that C is difficult to use with strings, learn how to use C.
C is rather difficult to use with strings, as we can see from this very example. Despite their arrogant confidence, the person who wrote it mixed up the various concepts and still has to learn how to use C.
That answer looks like someone managed to conflate string literals, which can be concatenated that way, with const
string variables. My guess is the original had preprocessor macros instead of variables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With