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.