C++ 错误:将“const char[5]”赋值给“char[10]”时类型不兼容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5414186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error:incompatible types in assignment of 'const char[5]' to 'char[10]'
提问by Ava
I have defined c as
我已经将 c 定义为
char c[][10]
in function definition and used it like c[i]="gray";
在函数定义中并使用它 c[i]="gray";
Whats wrong? I searched on net, it shows the same syntax.
怎么了?我在网上搜索,它显示了相同的语法。
Thanks.
谢谢。
回答by aschepler
You cannot use assignment (=) on an array. If you change cto an array of pointers, that might work, depending on what you need to do with it.
不能=在数组上使用赋值 ( )。如果您更改c为指针数组,这可能会起作用,具体取决于您需要对它做什么。
const char *c[20];
c[i] = "gray";
Or if the declared type must be array of arrays, you could use strncpy:
或者,如果声明的类型必须是数组数组,则可以使用strncpy:
char c[20][10];
strncpy(c[i], "gray", sizeof(c[i]));
回答by Adam Rosenfield
The problem is that arrays are not assignable in C. String constants like "gray"are character array constants: in this case, the type is char[5](4 + 1 for the terminating null).
问题是数组在 C 中是不可赋值的。字符串常量就像"gray"字符数组常量:在这种情况下,类型是char[5](4 + 1 表示终止空值)。
If you know that the destination array is large enough to hold the desired string, you can use strcpyto copy the string like so:
如果您知道目标数组足够大以容纳所需的字符串,您可以strcpy像这样复制字符串:
// Make sure you know that c[i] is big enough!
strcpy(c[i], "gray");
A better idea is to use a safer function such as strlcpy(BSD-based systems and Mac OS X) or strcpy_s(Windows):
更好的主意是使用更安全的功能,例如strlcpy(基于 BSD 的系统和 Mac OS X)或strcpy_s(Windows):
strlcpy(c[i], "gray", 10); // 10 is the size of c[i]
However, these functions are platform-specific and not all that portable. You could also roll your own implementation if speed is not an issue:
但是,这些功能是特定于平台的,并不是那么便携。如果速度不是问题,您也可以推出自己的实现:
size_t strlcpy(char *dst, const char *src, size_t size)
{
size_t len = 0;
while(size > 1 && *src)
{
*dst++ = *src++;
size--;
len++;
}
if(size > 0)
*dst = 0;
return len + strlen(src);
}
Do notuse strncpy, since it could potentially leave you with a non-null-terminated string
千万不能使用strncpy,因为它可能会留下一个非空结尾的字符串
回答by jonsca
Try using strcpy()(found in the cstringheader) instead of just plain assignment.
尝试使用strcpy()(在cstring标题中找到)而不是简单的赋值。
回答by tony gil
this code will work and make the correct assignements in 3 different ways:
此代码将以 3 种不同的方式工作并进行正确的分配:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
string myString = "hello my friends from Brazil";
char charOut[myString.size()];
strncpy(charOut, myString.c_str(), myString.size());
std::cout << "Char by strncpy string var " << charOut << std::endl;
const char *charOut2;
charOut2 = "sup my homies in L.A.";
std::cout << "Char by const " << charOut2 << std::endl;
string myString2 = "hallo mein bruder in Berlin";
char charOut3[myString2.size()];
strcpy(charOut3, myString2.c_str());
std::cout << "Char by strcpy string var " << charOut3 << std::endl;
}
runs ok on ubuntu servers. did not test on other systems.
在 ubuntu 服务器上运行正常。没有在其他系统上测试。

