简体   繁体   中英

An error in accessing a data member of a union

I keep getting an error message "error: incompatible types in assignment of 'const char[7] to [30]" when accessing the employee name. I know that only one data member of the union can be accessed at one time. Please, help. Thank you: Here is my code:

#include <iostream>
#include <string>
using namespace std;

union employee
{
    char name[30];
    int ID;
    float salary;
};
int main()
{
    union employee emp; 
    emp.name="Tibebu";
    cout<<"Employee name: "<<emp.name<<endl;
    return 0;
}

The problem isn't that the object is a member of a union. The problem is that you're trying to assign an array. And arrays are not assignable.

To modify an array, you must assign its elements. There are standard functions for copying multiple elements between arrays.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM