Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

This is linked to my previous post

Wher I created a Struct :

struct buffer
{
    char ProjectName[20];
       char ProjectID[20];
};

Now while I am trying to assign values to it:

buffer buf;
buf.ProjectID = "3174";
buf.ProjectName = "NDS";

I am getting this error:

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

and to resolve this I tried decreasing the size of structure as below(must not be the way to do it) :

struct buffer
{

    char ProjectName[4];
    char ProjectID[5];
};

and the get error C2106: '=' : left operand must be l-value

like image 345
Simsons Avatar asked Dec 03 '22 04:12

Simsons


1 Answers

You have to copy the string into the array:

strcpy(buf.ProjectName, "3174");

Be careful with the length of the strings being copied into the arrays

like image 119
jab Avatar answered Dec 08 '22 00:12

jab