c - 错误: c code: expression must be a modifiable lvalue

标签 c windows expression

我在 c 文件中有两个不同的结构,结构 A 和 B:

typedef Struct _A
{
   float arr[4];
}A;

typedef struct _B
{
   float const x;
   float const y;
}B;

 A* objA = (A*)malloc(sizeof(A));
 B* objB = (B*)malloc(sizeof(B));

我需要做的是用 struct B 中的值分配 arr 值

 objA->arr = {objB->x, objB->y, objB->x, objB->x};  /// getting an error here : expression must be a modifiable lvalue. 

到目前为止,我有 memcpy,但这会导致另一个错误“预期表达式”。 有什么办法可以做到这一点吗?

提前致谢!

最佳答案

您不能直接分配给数组。您需要单独分配给每个成员:

objA->arr[0] = objB->x;
objA->arr[1] = objB->y;
objA->arr[2] = objB->x;
objA->arr[3] = objB->x;

或者使用 memcpy 并以复合文字作为源:

memcpy(objA->arr, (float[4]){objB->x,objB->y,objB->x,objB->x}, sizeof(float[4]));

关于c - 错误: c code: expression must be a modifiable lvalue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68538706/

相关文章:

从 token 流解析表达式

C 运算符优先级 - 数组下标和前缀增量

haskell - 是否可以用 Haskell 或任何其他语言编写一个或多个仅表示封闭术语的数据结构?

c - 二进制搜索c代码错误

c - C语言中的optopt和getopt

c - 我对这个 if 语句的否定是否正确?

windows - 当前的 ankhsvn 版本与当前的 svn 兼容吗?版本

windows - 加载 PE 文件的资源时

c - 为什么我的程序结合了两个 printf 命令?

c++ - 以非管理员身份在 Windows 上检查 C 中的上次文件修改时间