Reputation: 37
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;
}
Upvotes: 1
Views: 4446
Reputation: 10182
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.
Upvotes: 0
Reputation: 29764
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 */
Upvotes: 0
Reputation: 62106
'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.]
Upvotes: 1
Reputation: 23727
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;
}
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.
Upvotes: 4
Reputation: 17938
Use strcpy(type, "pcs") and strcpy(type, "kgs") or std:string, you cant copy character in array simply by assigning!
Upvotes: 0
Reputation: 362197
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;
Upvotes: 5