Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C string concatenation of constants

Tags:

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.

like image 594
Frank Vilea Avatar asked Apr 24 '12 09:04

Frank Vilea


People also ask

Can you concatenate C style strings?

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.

Can you use += for string concatenation?

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.

Can you pass a string literal to strcat?

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.

What is string concatenation in C?

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.


2 Answers

(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.

like image 146
Lundin Avatar answered Sep 17 '22 15:09

Lundin


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.

like image 32
geekosaur Avatar answered Sep 20 '22 15:09

geekosaur