search

Home  >  Q&A  >  body text

c++中char[ ]的赋值问题

data.uData.serverinfo.iemi = pThis->imei;
data.uData.serverinfo.iemi是一个struct类型的 char imei[128];
pThis->imei 是一个private的char [128];在头文件中定义
但是编译的时候就报错
./Core/ServerManager.cpp:145:32: error: invalid array assignment

 data.uData.serverinfo.iemi = pThis->imei;
 

这是什么情况啊

PHPzPHPz2805 days ago641

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 13:38:53

    In c, the two parameters data.uData.serverinfo.iemi and pThis->imei do not refer to arrays, but represent array pointers, which record the location where data is stored in memory. Because data.uData.serverinfo.iemi is not an independent array, but an array in a structure, its location in memory is bound to the structure, which means you cannot directly assign the address of an external array to it. (The assignment is invalid, because even if no error occurs, the subsequent program will still access the array in the structure, which is the content of the original array), so it caused your error. If you want to copy the values ​​of an external array into an array in a structure, use the memcpy function. If you only want to pass the address, define this parameter as a pointer.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 13:38:53

    Assign directly? Shouldn't I use strcpy or memcpy? If you want to directly =, you should use std::string instead of char[].

    reply
    0
  • Cancelreply