0

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 able 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?

Foo Bah
  • 25,660
  • 5
  • 55
  • 79
liv2hak
  • 14,472
  • 53
  • 157
  • 270
  • Note that the standard reserves names ending with `_t` for future use. – Chris Lutz Oct 02 '11 at 23:05
  • 1
    Essentially a duplicate of [compilation error: request for member in something not a structure or union](http://stackoverflow.com/questions/7384581/compilation-error-request-for-member-in-something-not-a-structure-or-union) since unary `*` has the same precedence as a cast. Always Google your error messages. – Chris Lutz Oct 02 '11 at 23:08

4 Answers4

5

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
Foo Bah
  • 25,660
  • 5
  • 55
  • 79
  • 2
    I'd like to add that the reason for this is that `->` has higher precedence than casting, and that the OP should learn the C operator precedence table, or use more parenthesis. – Chris Lutz Oct 02 '11 at 23:06
3

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.

James
  • 9,064
  • 3
  • 31
  • 49
3

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 );
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • I have tried the below statement. ntohs(((record_t*)buffer)->len); with the parentheses at the right places.but that gives an error error: dereferencing type-punned pointer will break strict-aliasing rules. I may need to mention that buffer is uint8_t buffer[24]; – liv2hak Oct 02 '11 at 23:49
  • I did try len only.that was a typo :) – liv2hak Oct 02 '11 at 23:53
  • That's not an error, that's a warning. As I said, you're doing something very hackish and better understand the situation full well. I can only help with the operator precedence, if you don't want to post your actual problem. – Kerrek SB Oct 03 '11 at 00:03
  • got around the problem by declaring a record_t *rec; rec = (record_t*)buffer; and then doing rec->rlen.I can't figure out why it wouldn't work directly. – liv2hak Oct 03 '11 at 00:13
  • Post it as a separate question if you're curious. – Kerrek SB Oct 03 '11 at 00:16
  • http://stackoverflow.com/questions/7630150/dereferencing-type-punned-pointer-will-break-strict-aliasing-rules have posted a separate question – liv2hak Oct 03 '11 at 00:26
2

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

UncleO
  • 8,299
  • 21
  • 29