1

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;
    }
halachkin
  • 37
  • 1
  • 7
  • You can't do it like that even with the right type of literal. Use `std::string`. – chris Mar 28 '13 at 18:02
  • 1
    Well, depending on whether this is C or C++, std::string may not be an option. – Inisheer Mar 28 '13 at 18:02
  • 1
    `'pcs'` is a multi-character character constant, which has an implementation-defined value of type `int`. It's a language feature that's very rarely useful, but it can produce confusing error messages (or no error messages at all if you're particularly unlucky). – Keith Thompson Mar 28 '13 at 18:04
  • 2
    @Inisheer, If it's tagged C++, I feel free to use C++ in my responses. If the OP doesn't want C++, they shouldn't tag it as such, just like it isn't tagged Java, Python, C#, Ruby, or any other language they don't want. – chris Mar 28 '13 at 18:07
  • @chris Yup, agree. Just pointing it out in the event the OP wanted to use your comment while using C. – Inisheer Mar 28 '13 at 18:09

6 Answers6

5

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;
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 1
    @cf16: No, `strncpy` is *not* for strings. – md5 Mar 28 '13 at 18:07
  • @cf16: http://stackoverflow.com/questions/2114896/why-is-strlcpy-and-strlcat-considered-to-be-insecure (although it is sometimes used with strings, it is a bad idea). – md5 Mar 28 '13 at 18:10
  • ok, it might not null terminate, one should be aware of this, but for sure not strcpy in the form above! – 4pie0 Mar 28 '13 at 18:14
  • @cf16 `type` has room for five characters. These strings will occupy four (three chars + NUL). There is no danger. – John Kugelman Mar 28 '13 at 18:19
  • this is always some vulnerability – 4pie0 Mar 28 '13 at 18:21
  • if you find some way to interrupt strcpy and change strings that are to be copied then you are stuck, strncpy will not allow for this – 4pie0 Mar 28 '13 at 18:28
  • 1
    @cf16: Keith Thompson pretty much destroys the argument that `strncpy` is a *string* function [here](http://the-flat-trantor-society.blogspot.com/2012/03/no-strncpy-is-not-safer-strcpy.html). It's for dealing with fixed-size arrays of `char`, which are not necessarily strings. It is most definitely *not* a safer version of `strcpy`, and should not be used as such. – John Bode Mar 28 '13 at 18:50
  • @JohnBode really this is funny that paper: use strncpy as it is supposed to be used and this is really safe version of strcpy – 4pie0 Mar 28 '13 at 19:28
4

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.

md5
  • 23,373
  • 3
  • 44
  • 93
1

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

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
0

Use strcpy(type, "pcs") and strcpy(type, "kgs") or std:string, you cant copy character in array simply by assigning!

Saqlain
  • 17,490
  • 4
  • 27
  • 33
0

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 */
4pie0
  • 29,204
  • 9
  • 82
  • 118
  • 2
    You have to be very careful with `strncpy()`. It will not null terminate if it truncates. – Fred Larson Mar 28 '13 at 18:11
  • I have changed though i have to check this – 4pie0 Mar 28 '13 at 18:13
  • @FredLarson if you use string larger than buffer in vase of strcpy you are really out of luck but in case of strncpy it will be handled in safe way: what is better is obvious – 4pie0 Mar 28 '13 at 18:19
  • 1
    From [one man page reference](http://linux.die.net/man/3/strncpy): "Warning: If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated." This would also lead to undefined behavior. – Fred Larson Mar 28 '13 at 18:21
  • Of course, `strcpy()` does not have a problem with null termination. It has a problem with potential buffer overruns. It's hard to win with C string manipulation. Null termination, buffer overruns, [Schlemiel the Painter](http://www.joelonsoftware.com/articles/fog0000000319.html), etc. Lots of ways to get into trouble. – Fred Larson Mar 28 '13 at 18:35
0

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.

Mark
  • 10,022
  • 2
  • 38
  • 41