强制转换:左值作为赋值的左操作数

时间:2014-06-17 02:50:23

标签: 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

没有按&#39;吨

1 个答案:

答案 0 :(得分: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;