简体   繁体   English

错误C2440:“ =”:无法从“ int”转换为“ char [5]”

[英]error C2440: '=' : cannot convert from 'int' to 'char [5]'

error C2440: '=' : cannot convert from 'int' to 'char [5]' help me) 错误C2440:“ =”:无法从“ int”转换为“ char [5]”帮助我)

    char type[5];
    switch (rec[n-1].recptr->qtype)
    {
        case 'p':type='pcs'; break; //here is problem
        case 'm':type='kgs'; break; // and here is too
        default: printf("incorrect code");break;
    }

First, strings go in double quotes " , not single quotes ' . Second, to assign to a char[] array you must use a function like strcpy() . You can't assign directly to an array with = . 首先,串进去双引号" ,而不是单引号' 。其次,要分配给char[]数组你必须使用像函数strcpy()你不能直接分配到一个数组=

case 'p': strcpy(type, "pcs"); break;
case 'm': strcpy(type, "kgs"); break;

First, 'pcs' is a character constant, whereas you want a string. 首先, 'pcs'是一个字符常量,而您需要一个字符串。 The syntax is "pcs" . 语法为"pcs"

Moreover, type is an array, so when it is not used with sizeof , _Alignof or unary & operator, it decays to a pointer, and it is not an lvalue. 而且, type是一个数组,因此当不与sizeof_Alignof或一元&运算符一起使用时,它会衰减为指针,并且不是左值。 Therefore you cannot re-assign type . 因此,您不能重新分配type

strcpy could be a solution. strcpy可能是一个解决方案。

#include <string.h>

char type[5];

switch (rec[n-1].recptr->qtype)
{
    case 'p':
        strcpy(type,"pcs"); 
        break;
    case 'm':
        strcpy(type,"kgs"); 
        break;
    default: 
        printf("incorrect code");
        break;
}

Or, using string litterals (if you don't modify type ): 或者,使用字符串垃圾(如果您不修改type ):

const char *type;
switch (rec[n-1].recptr->qtype)
{
    case 'p':
        type="pcs"; 
        break;
    case 'm':
        type="kgs"; 
        break;
    default: 
        printf("incorrect code");
        break;
}

References 参考文献

C11 (n1570), § 6.3.2.1 Lvalues, arrays, and function designators C11(n1570),第6.3.2.1节L值,数组和函数指示符

Except when it is the operand of the sizeof operator, the _Alignof operator, or the unary & operator, or is a string literal used to initialize an array, an expression that has type ''array of type'' is converted to an expression with type ''pointer to type'' that points to the initial element of the array object and is not an lvalue. 除非它是sizeof运算符, _Alignof运算符或一元&运算符的操作数,或者是用于初始化数组的字符串文字,否则将类型为“ array of type”的表达式转换为带有键入“指向类型的指针”,它指向数组对象的初始元素,而不是左值。

'pcs' is a multi-character literal of type int . 'pcs'int类型的多字符文字。

type is an array. type是一个数组。 You cannot assign anything to an entire array with = . 您不能使用=将任何东西分配给整个数组。

[Technically speaking, in that expression type behaves as non-modifiable pointer pointing to the first element of the array, but you can't modify a non-modifiable value.] [从技术上讲,在该表达式type其行为相当于指向数组第一个元素的不可修改的指针,但是您不能修改不可修改的值。

使用strcpy(type,“ pcs”)和strcpy(type,“ kgs”)或std:string,您不能仅通过赋值来复制数组中的字符!

please use strcpy , you cannot assign to char[5] with = 请使用strcpy,您不能使用=分配给char [5]

case 'p': strcpy(type, "pcs"); break;

but if you want to avoid strcpy (even in such theoretically safe case) you can do it also this way: 但是如果您想避免使用strcpy(即使在这种理论上安全的情况下),也可以通过以下方式进行操作:

  /* partial copy (only 3 chars): */
  strncpy ( type, "pcs", 3 );
  type[4] = '\0';   /* null character manually added */

If you can keep to sizeof(int) characters or less, you can do something like the following: 如果您可以保持sizeof(int)个字符以下,则可以执行以下操作:

int type;
switch (rec[n-1].recptr->qtype)
{
    case 'p':type='pcs'; break; //here is problem
    case 'm':type='kgs'; break; // and here is too
    default: printf("incorrect code");break;
}

above code not tested, although I did test this: 上面的代码未经测试,尽管我确实对此进行了测试:

int main( int argc, char **argv)
{
    int t;
    t = 'abcd';
    printf ("t = %x\n", t);
    t = 'dcba';
    printf ("t = %x\n", t);
}

[347] ~/tmp: ./a.out
t = 61626364
t = 64636261

You really have to be careful here though that you don't use > sizeof(int) characters here. 尽管这里您不必使用> sizeof(int)字符,但您实际上必须要小心。 I suspect mileage may vary per compiler on what actually happens. 我怀疑每个编译器的实际运行情况可能会有所不同。 Using this method gets rid of all the extra string worries that are floating around in other answers. 使用此方法可以消除其他答案中所有多余的字符串担忧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误C2440:'=':无法从'const char [2]'转换为'char' - error C2440: '=' : cannot convert from 'const char [2]' to 'char' 错误C2440:&#39;=&#39;:无法从&#39;char [5]&#39;转换为&#39;char [20]&#39; - error C2440: '=' : cannot convert from 'char [5]' to 'char [20]' 错误C2440:&#39;=&#39;:无法从&#39;char *(__ cdecl *)(int,int)&#39;转换为en&#39;GetItemText_t&#39; - error C2440: '=' : cannot convert from 'char *(__cdecl *)(int,int)' to en 'GetItemText_t' 错误C2440:“ =”:无法从“ int *”转换为“ int **” - error C2440: '=' : cannot convert from 'int *' to 'int **' 错误C2440:“返回”:无法从“ int [2]”转换为“ int(&amp;&amp;)[2]” - Error C2440: 'return' : cannot convert from 'int [2]' to 'int (&&)[2]' decltype 错误 C2440 无法从“int *”转换为“int *&amp;” - decltype error C2440 cannot convert from 'int *' to 'int *&' 错误:C2440“返回”:无法从“int”转换为 T - error: C2440 'return': cannot convert from 'int' to T 错误c2440&#39;=&#39;无法从int *转换为Type <T> * - Error c2440 '=' cannot convert from int * to Type<T> * C2440:“ =”:无法从“ const char [9]”转换为“ char *” - C2440: '=': cannot convert from 'const char [9]' to 'char*' C2440&#39;=&#39;无法从&#39;int&#39;转换为&#39;BST <int> ::节点* - C2440 '=' cannot convert from 'int' to 'BST<int>::Node*
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM