C ,表达必须是可修改的LVALUE

C++, expression must be a modifiable lvalue

本文关键字:修改 LVALUE      更新时间:2023-10-16

我有以下代码:

#include "stdafx.h"
#include<iostream>
using namespace std;
const int x = 5;
bool graf_adj[x][x] = {
0,1,1,1,0,
1,0,1,0,0,
1,1,0,1,1,
1,0,1,0,0,
0,0,1,0,0
};
struct Graf
{
    bool adj[x][x];
    char n;
};
int main(){
Graf graf1;
graf1.adj = graf_adj;
}

当我尝试将graf_adj与graf1.adj串起时,在主要功能中 graf1.adj = graf_adj; complier给我这个错误:

错误表达式必须是可修改的lvalue

任何人都可以解决这个问题吗?

谢谢

现在您添加了const的类型:

这是使用memcpy

的解决方案
#include<iostream>
#include <cstring>
const int x = 5;
bool graf_adj[x][x] = {
0,1,1,1,0,
1,0,1,0,0,
1,1,0,1,1,
1,0,1,0,0,
0,0,1,0,0
};
struct Graf
{
    bool adj[x][x];
    char n;
};
int main(){
Graf graf1;
std::memcpy(&graf1.adj, &graf_adj, sizeof(graf1.adj));
}