简体   繁体   中英

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

error C2440: '=' : cannot convert from 'int' to 'char [5]' help me)

    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 = .

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

First, 'pcs' is a character constant, whereas you want a string. The syntax is "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. Therefore you cannot re-assign type .

strcpy could be a solution.

#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 ):

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

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.

'pcs' is a multi-character literal of type int .

type is an array. 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.]

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

please use strcpy , you cannot assign to char[5] with =

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:

  /* 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:

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM