I have a variable with type wchar (szDrive), now i want to have a array and element of it has type wchar. Here is some my code:

typedef struct array_wchar{
WCHAR array_char[5];
};
array_wchar array_driveName0[10];
int array_driveName_index0 =0;
WCHAR szDrive[5] = L" :\\";

but when i write:

for(int i=0;i<10;i++){
    array_driveName1[i].array_char = szDrive;
}

it has an error: error C2106: '=' : left operand must be l-value

Can someone explain me why and can give resolution? plz!

有帮助吗?

解决方案

You can't assign directly to an array like that. You need to either use memcpy / strcpy or assign to individual elements:

memcpy(array_driveName0[i].array_char, szDrive, sizeof(array_driveName0[i].array_char));

or

for (j = 0; j < 5; j++)
{
    array_driveName0[i].array_char[j] = szDrive[j];
}

Also you seem to be referencing array_driveName1 where you have only declared array_driveName0.

Your overall structure is very confusing. You have an array of structs, each of which contains only an array of WCHARs - why not just have an array of arrays and remove the struct entirely?

For example:

typedef WCHAR array_char_t[5];
array_char_t array_driveName0[10];
WCHAR szDrive[5] = L" :\\";

memcpy(array_driveName0[i], szDrive, sizeof(array_driveName0[i]));
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top