I'm going to implement a singly linked list program with bitfield in its structure, something like this:
typedef struct large
{
unsigned number :4;
struct large *next;
} large;
int main()
{
large *g;
g=(large *)malloc(sizeof(large));
g->number=15;
printf("%d",g->number);
return 0;
}
The above program is working correctly but I read in GeeksForGeeks that,
We cannot have pointers to bit field members as they may not start at a byte boundary.
Will there be problems if I code it further for linked list implementation?
The pointer g
in your program is a pointer to a structure variable to which you allocated memory dynamically.
g->number
is not the address of the member number
but its value.
You cannot have a pointer like
unsigned char *ptr=&(g->number);
you should get an error like cannot take address of bit-field
malloc-ing any structures including those containing bitfields is absolutely OK
We cannot have pointers to bit field members as they may not start at a byte boundary.
But you do not get the address of the bitfield - only for the structure itself and its size and location is always a multiple of byte.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.