Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate const char* strings in c++ with no function calls?

Ps: This is more of a conceptual question.

I know this makes things more complicated for no good reason, but here is what I'm wondering. If I'm not mistaken, a const char* "like this" in c++ is pointing to l and will be automatically zero terminated on compile time. I believe it is creating a temporary variable const char* to hold it, unless it is keeping track of the offset using a byte variable (I didn't check the disassembly). My question is, how would you if even possible, add characters to this string without having to call functions or instantiating strings?

Example (This is wrong, just so you can visualize what I meant):

"Like thi" + 's';

The closest thing I came up with was to store it to a const char* with enough spaces and change the other characters.

Example:

char str[9];
strcpy(str, "Like thi")
str[8] = 's';

Clarification: Down vote: This question does not show any research effort; it is unclear or not useful

Ok, so the question has been highly down voted. There wasn't much reasoning on which of these my question was lacking on, so I'll try to improve all of those qualities.

My question was more so I could have a better understanding of what goes on when you simply create a string "like this" without storing the address of that string in a const char* I also wanted to know if it was possible to concatenate/change the content of that string without using functions like strcat() and without using the overloaded operator + from the class string. I'm aware this is not exactly useful for dealing with strings in C++, but I was curious whether or not there was a way besides the standard ways for doing so.

string example = "Like thi" + "s"; //I'm aware of the string class and its member functions
const char* example2 = "Like this"; //I'm also aware of C-type Strings (CString as well)

It is also possible that not having English as my native language made things even worst, I apologize for the confusion.

like image 229
Artie Avatar asked Dec 12 '11 03:12

Artie


People also ask

Can you concatenate a string and a char in C?

Use the strncat() function to append the character ch at the end of str. strncat() is a predefined function used for string handling. string.

How can I add two strings without using operator?

You can use str. join(list_of_strings) from the string class. The string you call it on is used to join the list of strings you provide as argument to it.

What is the most efficient way to concatenate many strings together?

If you are concatenating a list of strings, then the preferred way is to use join() as it accepts a list of strings and concatenates them and is most readable in this case. If you are looking for performance, append/join is marginally faster there if you are using extremely long strings.


2 Answers

Instead of using a plain char string, you should use the string library provided by the C++ library:

#include <string>
#include <iostream>
using namespace std;

int main()
{
    string str = "Like thi";
    cout << str << endl;

    str = str + "s";
    cout << str << endl;
    return 0;
}

Normally, it's not possible to simply concatenate plain char * strings in C or C++, because they are merely pointers to arrays of characters. There's almost no reason you should be using a bare character array in C++ if you intend on doing any string manipulations within your own code.

Even if you need access to the C representation (e.g. for an external library) you can use string::c_str().

like image 177
Mike Bailey Avatar answered Sep 27 '22 17:09

Mike Bailey


First, there is nothing null terminated, but the zero terminated. All char* strings in C end with '\0'.

When you in code do something like this:

char *name="Daniel";

compiler will generate a string that has a contents:

Daniel\0

and will initialize name pointer to point at it at a certain time during program execution depending on the variable context (member, static, ...).

Appending ANYTHING to the name won't work as you expect, since memory pointed to by name isn't changeable, and you'll probably get either access violation error or will overwrite something else.

Having

const char* copyOfTheName = name;

won't create a copy of the string in question, it will only have copyOfTheName point to the original string, so having

copyOfTheName[6]='A';

will be exactly as

name[6]='A';

and will only cause problems to you.

Use std::strcat instead. And please, do some investigating how the basic string operations work in C.

like image 30
Daniel Mošmondor Avatar answered Sep 27 '22 16:09

Daniel Mošmondor