1

First I convert my string to type char

//Convert string to char
string temp = "TFTFTFTFTFFFTTFFTTFFTTTTTTFFFF";   
string temp = line;
char tab2[1024];
strcpy(tab2, temp.c_str());
    
cout << "The value of the string conversion is: " << tab2 <<endl;

Then I attempt to assign the tab2 contents to the char array:

char Array[] = {tab2};

Error: Cannot initialize an array element of type 'char' with an lvalue of type 'char [1024]

Steve Jobs
  • 63
  • 1
  • 1
  • 7
  • Does this answer your question? [How to copy a char array in C?](https://stackoverflow.com/questions/16645583/how-to-copy-a-char-array-in-c) – guimauve Nov 13 '20 at 05:50
  • I don’t concur that’s an appropriate duplicate, even if it may lead to a viable program, as it doesn’t explain why this particular syntax/error occurs — and there be differences between C and C++ in some unexpected places. This question is also tagged C++ so there are better ways of dealing with strings in context. – user2864740 Nov 13 '20 at 05:57
  • 1
    Seems more like why can't I initialize an array with another array than a string question. – Retired Ninja Nov 13 '20 at 06:01
  • 1
    Perhaps use `char *Array = tab2;` instead? Depends on the higher level coding goal. – chux - Reinstate Monica Nov 13 '20 at 06:05
  • You could easily use a `std::vector` for this, but it isn't really clear what the goal is. The error you're getting is because you simply can't do that. – Retired Ninja Nov 13 '20 at 06:24

2 Answers2

2

What happens in your case is that you are trying to initialize a c-style array with another c-style array.

C-style arrays can't be initialized in this manner nor can they be copied using a regular copy assignment.
To copy it using the copy assignment would require looping through and copying one element at a time and actually assigning a size to the second array.

There are two more practical approaches for your problem. Either use std::array<> or another container type, instead of a char array, or use memcpy

For the former approach you'll just have to replace your arrays to type std::array<> and then you can initialize or copy with the regular copy assignment as you like.

For the latter approach you can use memcpy() to copy the memoryblock from the first c-style array to the other. Like this:

Read these reference pages on memcpy: http://www.cplusplus.com/reference/cstring/memcpy https://en.cppreference.com/w/cpp/string/byte/memcpy

{
    /* ... */
    char new_tab[1024] = {0}; 
    std::memcpy(new_tab, prev_tab, sizeof(char)*1024);
    std::cout << "The value after copy is: " << new_tab << std::endl;
    /* ... */
}

Edit:

As your intent with this was not clear, but lets assume it is an assignment and you have to convert to an array and then copy the array to another array. In that case read this for reference: https://en.cppreference.com/w/cpp/string/byte/strncpy http://www.cplusplus.com/reference/cstring/strncpy/

And then keep things as you had them but just change your char Array[] = {tab2}; To:

{
    /* ... */
    char new_tab[1024];
    // Copies 1024 characters from prev_tab to new_tab
    std::strncpy(new_tab, prev_tab, 1024);
    /* ... */
}

Note: Also edited and added some reference links

Ario Amin
  • 91
  • 7
  • Thanks Man this worked well! I'm still learning C++ doing this project for a college class. – Steve Jobs Nov 13 '20 at 07:25
  • This seemed to modify the C++ system file ostream on my Macontosh and now the program breaks at build time, so maybe not the best solution. – Steve Jobs Nov 16 '20 at 22:49
  • @JeremiahFries What error are you getting at build? A segfault? The first option I mentioned with vectors is memory-safe, on the the second option you need to make sure you are not writing to read-only memory. Maybe you are accidentally copying memory directly to the ostream? For reference baout memcpy: https://en.cppreference.com/w/cpp/string/byte/memcpy . Either way going with a predefined container like `std::string` `std::vector` or `std::array` would be a safer option for you compared to using c-style arrays and copying memory manually. – Ario Amin Nov 16 '20 at 23:19
  • @JeremiahFries In case you can't use any container above as a restriction of some sort, then I have added and edit to the answer which would suffice. Just keep this in mind regarding strncpy: "No null-character is implicitly appended at the end of destination if source is longer than num. Thus, in this case, destination shall not be considered a null terminated C string (reading it as such would overflow)." http://www.cplusplus.com/reference/cstring/strncpy/ – Ario Amin Nov 17 '20 at 00:15
0

You cannot do

 char Array[] = {tab2};

because that expects expression tab2 to be bound to an element of array as per initialization list syntax. You cannot do

 char Array[] = tab2;

either , because name of array tab2 decays to a pointer to its first element. Very same problem exists in C and it can be solved in same ways. Either copy them element by element, use memcpy or strcpy or C++ idiomatic std::copy, or use array as part on aggregate. But C++ got an aggregate you can use in <array>:

std::array<char,1024> tab2;

// we really have to check length there, you can overflow this buffer 

strcpy(tab2.data(), temp.c_str());

auto Array = tab2;

A struct that allow a shallow copy will do too:

struct S {
    char _data[1024];
    char* data() { return _data; }
};

Shallow copy performed on S or on std::array copies storage of every element of source to destination. This cannot be used with pointers because the pointer will be copied, not the storage it is pointing at.

That's essentially a model of std::array<char,1024> minus its bells and whistles.

Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42