char types[5];
switch(buffer[9]){
case 1:
types = "ICMP";
break;
error:
incompatible types when assigning to type 'char[5]' from type 'char *'
how would I fix this error?
我该如何解决这个错误?
Thanks.
1 个解决方案
#1
You can't fix it without changing its type.
如果不更改类型,则无法修复它。
Array values are not modifiable l-values. If you plan to always assign it a string literal, declare it as const char *
instead of array:
数组值不是可修改的l值。如果您计划始终为其分配字符串文字,请将其声明为const char *而不是array:
const char *types;
switch(buffer[9]){
case 1:
types = "ICMP";
break;
If you want to later change the contents of types
, then keep it as an array and just use strcpy()
or the safer variant strncpy()
:
如果您想稍后更改类型的内容,请将其保存为数组,并使用strcpy()或更安全的变量strncpy():
char types[5];
switch(buffer[9]){
case 1:
strncpy(types, "ICMP", sizeof(types));
break;
#1
You can't fix it without changing its type.
如果不更改类型,则无法修复它。
Array values are not modifiable l-values. If you plan to always assign it a string literal, declare it as const char *
instead of array:
数组值不是可修改的l值。如果您计划始终为其分配字符串文字,请将其声明为const char *而不是array:
const char *types;
switch(buffer[9]){
case 1:
types = "ICMP";
break;
If you want to later change the contents of types
, then keep it as an array and just use strcpy()
or the safer variant strncpy()
:
如果您想稍后更改类型的内容,请将其保存为数组,并使用strcpy()或更安全的变量strncpy():
char types[5];
switch(buffer[9]){
case 1:
strncpy(types, "ICMP", sizeof(types));
break;