[英]How do I assign a char* to a char array?
Compiler tell me "incompatibles type in assignments of char* to char[32]" this is my code: 编译器告诉我“在char *到char [32]的分配中输入不兼容类型”,这是我的代码:
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]);
Use std::string
instead of char*
and std::array<T, N>
instead of T[N]
. 使用std::string
代替char*
和std::array<T, N>
代替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. 两者都是类型安全的(与memcpy
相对),都是现代C ++风格,并且都可以使用赋值运算符直接赋值。
#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;
}
}
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
. 您可以将value
的类型更改为char*
也可以将generalOptions[0]
的内容复制到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
. 如果要复制内容,则需要确保value
具有足够的空间来保存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
一项进行修改的任何尝试,而不是在运行时遇到奇怪的行为:
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. 或者,如果这真的是C ++可以使value
一个std::string
代替,只是分配给它的将复制generalOperations
元素。
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
: 由于C ++确实是该语言,并且允许使用C ++ 11功能而不是使用switch
您可以创建一个将int
值与std::string
关联的std::map
:
#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 . 演示: http : //ideone.com/rvFxH 。
#include <string.h>
...
strcpy(value, generalOptions[0]);
You cannot assign arrays in C/C++. 您不能在C / C ++中分配数组。 There are functions do to that for you. 有一些功能可以帮助您。 If your char array represents a C style string (ie a null terminated sequence of characters), then there are more specialist functions for that as well. 如果您的char数组表示C样式字符串(即,以null终止的字符序列),则还有更多专用功能。 strcpy
is one of those functions. strcpy
是这些功能之一。
您的分配是错误的,因为不能将char *分配给char数组,而不是使用此分配,则可以使用strcpy()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.