类型转换 : lvalue required as left operand of assignment

标签 c casting compiler-errors gpu c99

我只是在没有完整内存管理的系统上使用此代码:

typedef unsigned short component_t;
typedef struct {
    component_t* c;    // least-significant word first
    unsigned int num_components; // number of unsigned short rows
} integer;

integer result;
result.c=malloc(component_t*)malloc(sizeof(component_t)*128); //this is just an example to tell I'm correctly initializing. There's no malloc nor memset inside OpenCL. currently, I'm trying to speed up the code serially before switching to OpencL. That's also why I don't use GMP.
result.num_components=128

下一行:

for(int i=0;i<result.num_components/4;i++) (unsigned long)result.c[i] = 26; // assign 26 in that part of the memory

触发:

gcc integer.c
integer.c:567:36 error: lvalue required as left operand of assignment
     (unsigned long)result.c[5] = 26;
                                ^

我不知道该行的真正问题以及我到底需要写什么来纠正这个问题。
注意:我也见过(即使这没用):

for(int i=0;i<result.num_components/4;i++) (unsigned long)result.c[i]++

编译时

for(int i=0;i<result.num_components/4;i++) (unsigned long)result.c[i]+=1

没有。

最佳答案

integer.c:567:36 error: lvalue required as left operand of assignment
     (unsigned long)result.c[5] = 26;
                                ^

result.c[5]的值被检索并转换为unsigned long ,这会给您留下类似于 15 = 26 的分配。您(大概)想要的是转换 result.c指向 unsigned long指针 :

 ((unsigned long *)result.c)[5] = 26;

关于类型转换 : lvalue required as left operand of assignment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24255019/

相关文章:

javascript - 具有构造函数以及 HTML 和 CSS 文件的组件数组。错误 NG2003。 ngFor

c - 为什么程序返回访问冲突?

c - 读取 '\n' 后 fgets 意外行为

c# - 将 int 上的无效转换异常加倍

casting - 在 Go 中对多个返回值进行转换/类型断言的惯用方法

c++ - 复制构造函数错误 : returning a value from a constructor

c++ - 将 tr1::regex 与 unicode 字符串一起使用时出现问题

c++ - 多个 DLLMain 函数

C 字符串数组操作

delphi - 使用 (Object as TClass) 和 TClass(Object) 进行强制转换有什么区别