-3

I have a struct that is this:

#define REQUEST_SIZE_BITS 16
#define HID_SIZE_BITS 32
#define BLOCK_SIZE_BITS 16
#define FID_SIZE_BITS 32
typedef struct __attribute__((__packed__)) footer {
    uint64_t     block_size : BLOCK_SIZE_BITS;
    uint64_t            fid : FID_SIZE_BITS;
    uint64_t requested_size : REQUEST_SIZE_BITS;
} footer;

But when I create a instance of this struct

footer prologue;
prologue->block_size = 16;

The compiler says that Invalid type argument of "->" Why is this happening?

Should I use

prologue.block_size = 16;

Instead of using prologue->block_size?

Also, what does

block_size : BLOCK_SIZE_BITS;

do when we define this struct? Is it assigning it a default value?

  • 1
    Please refresh your text-books about structures and how to access members of them. The "arrow" operator are for *pointers* to structures. – Some programmer dude Nov 11 '21 at 12:37
  • `prologue` is **not** a pointer of type `footer`. Access `block_size` using `.` operator - `prologue.block_size`. – H.S. Nov 11 '21 at 12:38
  • sorry for confusing about question on ```->``` operator, I edited the question – randomNameGenerator Nov 11 '21 at 12:39
  • 1
    Please don't mix 2 completely different topics into 1 question. – Gerhardh Nov 11 '21 at 12:41
  • If you already found a solution for Q1 and your compiler probably does not complain any more after that change, what remains unclear? – Gerhardh Nov 11 '21 at 12:42
  • I think you're trying to get too far ahead of your own knowledge. While bitfields are not common (but should still be referenced in any decent book or class) the two ways to access structures (with the `.` for structure object, or with `->` for pointers to structure objects) should be well-known. Please take a few steps back, and refresh some of the basic of C. – Some programmer dude Nov 11 '21 at 12:48
  • @Someprogrammerdude I agree. OP needs to learn C first before going into implementation defined stuff like packed bitfields – 0___________ Nov 11 '21 at 12:49

1 Answers1

0

The -> operator is used to access a struct member via a pointer to a struct. When you have an instance of a struct, you instead use the . operator.

So if you had this:

footer f;
footer *p = &f;

You would use this:

f.block_size = 16;
p->block_size = 16;
dbush
  • 205,898
  • 23
  • 218
  • 273