开发者

gcc: error: request for member ‘rlen’ in something not a structure or union

开发者 https://www.devze.com 2023-04-10 08:20 出处:网络
I have a unsigned char pointer which contains a structure.Now I want to do the following unsigned char *buffer ;

I have a unsigned char pointer which contains a structure.Now I want to do the following

unsigned char *buffer ;

//code to fill the buffer with the relavent information.

int len = ntohs((record_t*)buffer->len);

where record_t structure contains a field called len.I am not abl开发者_C百科e to do so and am getting the error.

error: request for member ‘len’ in something not a structure or union.

what am I doing wrong here?


in C you can't just take buffer->len, because it's being parsed as if the final result buffer->len is being cast to a record_t *. Try

((record_t *)buffer)->len


If you're confident that you're doing the right thing (though this looks very hackish), you just have to get the operator precedence right:

ntohs( ((record_t*)buffer)->len );


Try ((record_t*)buffer)->len

You're casting buffer->len to a record_t*, when what you want to do is cast buffer to a record_t and then get the len value of that.


Precedence of -> is higher than the cast. Add some parentheses appropriately.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号