2

I'm new to C++, and I'm having trouble to concatenate a const char* with a char aray.  

First, I'd like to ask a question: Is const char* the same as std::string?

And going straight to my problem, this is what I'm trying:

I have this 10 sized char array that contains letters from a to j:

char my_array[10] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', j};

And I want to store this array (or part of it), using a const char*. But I could not do the concatenation, I thought it would be some think like this:

    const char* group_of_letters = "\0";
    
    for(int i = 0; i <= 9; i++){
        group_of_letters += my_array[i];
    }

I'd like to use the for loop because I'll need it on my real project to store defined intervals of that array into the const char* But it didn't work any way that I have tried.

If it helps, my complete code would look like this:

#include <iostream>

int main(){
    
    char my_array[10] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
    const char* group_of_letters = "\0";
    
    for(int i = 0; i <= 9; i++){
        group_of_letters += my_array[i];
    }
    
    std::cout << group_of_letters;
    
    return 0;
}

->Using Dev C++ 5.11 - 06/08/2023

kuybt6
  • 23
  • 5
  • 3
    Looks like you really want to use `std::string`. A pointer is just a pointer, it doesn't "store" anything. If you want to copy characters you need memory, and messing with memory allocation is a pain. `std::string` handles that for you. – Etienne de Martel Aug 06 '23 at 03:21
  • `const char* group_of_letters = "\0";` That makes `group_of_letters` point to the constant array of two characters containing two null-terminators. `group_of_letters += ...` increases the *pointer*, it doesn't append to the constant array it points to. You could make `group_of_letters` point to the array `my_array` instead: `const char* group_of_letters = my_array;`. But note that `my_array` isn't actually a "string" because it doesn't have a null-terminator, so when you print the "string" later you will have *undefined behavior*. – Some programmer dude Aug 06 '23 at 03:26
  • The simple solution to all your problems, as mentioned above, is to use `std::string` to store strings. And if you want a *view* into a string use [`std::string_view`](https://en.cppreference.com/w/cpp/string/basic_string_view). – Some programmer dude Aug 06 '23 at 03:33
  • The `std::string` worked! But now I'm having trouble sending that std::String through a socket: `[Error] cannot convert 'std::string {aka std::basic_string}' to 'const char*' for argument '2' to 'int send(SOCKET, const char*, int, int)'`. Will I need to create an other `const char*` to store that `std::String` and then use it? – kuybt6 Aug 06 '23 at 03:43
  • Always ask about the actual problem you need to solve, directly. Don't ask about the problem you have with your solution to the problem. We might have other possible solutions. If you don't ask about the actual problem you need to solve, then your question becomes an [XY problem](https://en.wikipedia.org/wiki/XY_problem). – Some programmer dude Aug 06 '23 at 04:11

2 Answers2

3

First, I'd like to ask a question: Is const char* the same as std::string?

No they are not the same, const char* is a pointer to a character sequence in C-style strings, while std::string is a C++ class that manages and manipulates strings with automatic memory management.

You can't concatenate a char* array using the + operator.

In your case it's better to use std::string as below:

#include <iostream>
#include <string>

int main(){
    
    char my_array[10] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
    std::string group_of_letters;
    
    for(int i = 0; i <= 9; i++){
        group_of_letters += my_array[i];
    }
    
    std::cout << group_of_letters << std::endl;
    
    return 0;
}
wohlstad
  • 12,661
  • 10
  • 26
  • 39
  • The std::string worked! But now I'm having trouble sending that `std::String` through a socket: `[Error] cannot convert 'std::string {aka std::basic_string}' to 'const char*' for argument '2' to 'int send(SOCKET, const char*, int, int)`. Will I need to create an other `const char*` to store that `std::String` and then use it? – kuybt6 Aug 06 '23 at 03:47
  • 1
    I used string.c_str() and it worked, thanks to all of u (; – kuybt6 Aug 06 '23 at 03:56
0

std::string is far different from a constant string literal. It is the case that std::string internally uses its own allocator to allocate needed memory space to construct a string from its constructor argument. You can access a C-style pointer to std::string's buffer via c_str function.

const char*, in fact must be interpreted as a pointer to constant char and not a constant pointer to char, because you would be able to increment and decrement the pointer, which shows the fact that it is being a pointer to a non-modifiable block of memory. What you make when you write something like

const char* str = "This is a test" 

is that in memory, there is a block where all characters of "This is a test" laid down there sequentially and indeed this block is in .rodata section or read-only section of the final object file.

You define a pointer named str to point to this block of memory, and you can modify the pointer itself by something like str++ or str-- but cannot modify its content. Therefore something like *(str) = 'k' would give you a Segmentation Fault error.

std::string is a C++ class which encapsulates the string you made and gives access to many different class methods where you are able to get informed about different properties and values of the string. As mentioned, you can access the C-style const char* buffer by c_str, access the size of string by size, and many more. Just look here for more information.

Back to your question, I would recommend considering something like the following:

#include <iostream>
#include <string>
#include <array>

int main()
{

    std::array<char, 10> my_array = {
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'
    };

    std::string group_of_letters;

    for (const auto& ch: my_array)
        group_of_letters += ch;

    std::cout << group_of_letters;

    return 0;
}