1

Compiler tell me "incompatibles type in assignments of char* to char[32]" this is my code:

char* generalOperations[2]={"Off","On"};

 void test(){
   char value[32];
    switch(swapVariable){
     case 0:
      value=generalOperations[0]; //<==Error HERE!
     break;
    }

 }

[Solved]:

  strcpy(value,generalOperations[0]);
Augusto Picciani
  • 788
  • 2
  • 11
  • 31

4 Answers4

8

Use std::string instead of char* and std::array<T, N> instead of T[N]. Both are type safe (as opposed to memcpy), both are in modern C++ style and both are directly assignable using the assignment operator.

#include <array>
#include <string>

std::array<std::string, 2> generalOperations{"Off", "On"};

void test() {
    std::string value;
    switch(swapVariable) {
        case 0: value = generalOperations[0]; break;
    }
}
daknøk
  • 646
  • 5
  • 11
3

You can't assign arrays. You can either change the type of value to a char* or copy the content of generalOptions[0] into value. If you are going to copy the content, then you need to ensure that value has enough space to hold the content of the element in generalOperations.

Modifying a string literal is undefined behaviour, by changing the type to const char* the compiler can detect any attempt to modify one of the entries in generalOperations instead of experiencing odd behaviour at runtime:

const char* generalOperations [2]={"Off","On"};

const char* value;

Note you don't have to specify the number of elements in the array if you are initialising it:

const char* generalOperations [] = {"Off","On"};

Or, if this really is C++ you can make value a std::string instead and just assign to it which will copy the generalOperations element.


As C++ appears to really be the language and C++11 features are permitted instead of using a switch you could create a std::map that associates the int values with the std::string:

#include <iostream>
#include <string>
#include <map>

const std::map<int, std::string> generalOperations{ {17, "Off"},
                                                    {29, "On" } };
int main()
{
    auto e = generalOperations.find(17);
    if (e != generalOperations.end())
    {
        // Do something with e->second.
        std::cout << e->second << "\n";
    }
    return 0;
}

Demo: http://ideone.com/rvFxH.

hmjd
  • 120,187
  • 20
  • 207
  • 252
  • I would also recommend making char* generalOperations a const char*. Treating char* as a string literal can cause warnings on most compilers. – Downie Oct 19 '12 at 14:21
  • @DeadMG, its mentioned in the answer. Additionally, I think the question is mistagged as there is nothing specific to C++ whatsoever. – hmjd Oct 19 '12 at 15:02
  • You mean like people using C++ as C with Classes? – Rapptz Oct 19 '12 at 15:04
0
#include <string.h>

...
strcpy(value, generalOptions[0]);

You cannot assign arrays in C/C++. There are functions do to that for you. If your char array represents a C style string (i.e. a null terminated sequence of characters), then there are more specialist functions for that as well. strcpy is one of those functions.

john
  • 7,897
  • 29
  • 27
  • 1
    Wow, I'm not going to dignify that comment with an answer. – john Oct 19 '12 at 15:13
  • @john: Instead, you could dignify it with picking up [a good C++ book](http://stackoverflow.com/q/388242/140719) and learn something. – sbi Oct 20 '12 at 10:28
  • 1
    @ubi I know C++, I've read the first 6 books on that list you helpfully provided (excluding the C++ 11 ones). I tailor my answers to what I think will be helpful to the OP, I don't automatically answer with the highest powered C++ I can manage. That's not going to help many of the newbies who post here. I'll grant that the above isn't my best ever answer since I should have seen that the OPs attempt to copy arrays was a good oppotunity to introduce std::string precisely because it can be copied in the way the OP was trying with arrays. – john Oct 20 '12 at 17:35
-1

Your assignment is wrong, since you cannot assign a char * to char array instead of using this assignment you can use strcpy().

AAA
  • 348
  • 1
  • 4
  • 19