Questions tagged [offsetof]

Anything related to the C and C++ `offsetof` macro. `offsetof` is used to determine the offset in bytes of a structure member from the beginning of the structure itself.

Anything related to the C and C++ offsetof macro. offsetof is used to determine the offset in bytes of a structure member from the beginning of the structure itself.

See CPPreference.com documentation for offsetof.

88 questions
9
votes
3 answers

&((struct name *)NULL -> b) in printf statement

I found this code sample in a book, but I am unable to understand the expression in printf statement. and this program compiles successfully giving output as 4. kindly advise... void main(){ unsigned char c; typedef struct name { long…
Himanshu Sourav
  • 700
  • 1
  • 10
  • 35
9
votes
3 answers

offsetof at compile time

Is there a way of finding the offset of a member of a structure at compile-time? I wish to create a constant containing the offset of a structure member. In the following code the offsetof() macro works in the first printf statement. However, the…
Dr. Paul
7
votes
1 answer

Boost Fusion Types offsetof

I'm currently trying to calculate an offset of a data member in a boost fusion adapted structure, but I am not sure if there is an elegant way to do so. I'd like to do something like the following: #include #include…
user985030
  • 1,557
  • 1
  • 16
  • 32
6
votes
2 answers

Is accessing members through offsetof well defined?

When doing pointer arithmetic with offsetof, is it well defined behavior to take the address of a struct, add the offset of a member to it, and then dereference that address to get to the underlying member? Consider the following example: #include…
Ben Steffan
  • 1,095
  • 12
  • 16
6
votes
5 answers

Is apparent NULL pointer dereference in C actually pointer arithmetic?

I've got this piece of code. It appears to dereference a null pointer here, but then bitwise-ANDs the result with unsigned int. I really don't understand the whole part. What is it intended to do? Is this a form of pointer arithmetic? struct hi …
karthik A
  • 655
  • 1
  • 11
  • 19
6
votes
4 answers

How to set structure element at desired offset

In embedded programming when describing the hardware one often needs to place struct elements at known predefined positions as the HW engineer designed them. For example, let's define a structure FPGA, which has some 100 registers/areas as in the…
leonp
  • 487
  • 5
  • 22
6
votes
2 answers

Why subtract null pointer in offsetof()?

Linux's stddef.h defines offsetof() as: #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) whereas the Wikipedia article on offsetof() (http://en.wikipedia.org/wiki/Offsetof) defines it as: #define offsetof(st, m) \ ((size_t) (…
Bruce Christensen
  • 1,564
  • 1
  • 11
  • 12
5
votes
1 answer

Why offsetof implementations strangely differs on C and C++?

I opened stddef.h and saw this: #if defined _MSC_VER && !defined _CRT_USE_BUILTIN_OFFSETOF #ifdef __cplusplus #define offsetof(s,m) ((size_t)&reinterpret_cast((((s*)0)->m))) #else #define offsetof(s,m)…
Artem Selivanov
  • 1,867
  • 1
  • 27
  • 45
5
votes
3 answers

Struct offsets and pointer safety in C++

This question is about pointers derived using pointer arithmetic with struct offsets. Consider the following program: #include #include #include struct A { float a; double b; int c; }; static constexpr auto off_c =…
Chris Beck
  • 15,614
  • 4
  • 51
  • 87
5
votes
1 answer

Difficulty in understanding the offsetof MACRO

I have been searching very long and hard (links at the very end) for an explanation of the implementation of the offsetof MACRO : #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) Particularly, the dereferencing of NULL to obtain the…
maverick
  • 63
  • 5
5
votes
3 answers

Why is offsetof(member) equal to sizeof(struct)?

I have a struct defined as: struct smth { char a; int b[]; }; When I call sizeof and offsetof on this struct: cout << sizeof(struct smth) << endl; cout << offsetof(struct smth, b) << endl; Output is: 4 4 How come when the size of the…
bugra
  • 55
  • 5
4
votes
1 answer

Does offsetof require pointer derefence?

I am wondering whether a simple macro offset_of_ requires a pointer dereference of not. For example, a C++ (means that this code will be compiled using a C++ compiler) struct which is declared with packed attribute struct A { int x; int y; }…
Ta Thanh Dinh
  • 638
  • 1
  • 5
  • 12
4
votes
1 answer

C - Reference after dereference terminology

This question is about terminology. int main() { unsigned char array[10] = {0}; void *ptr = array; void *middle = &ptr[5]; // <== dereferencing ‘void *’ pointer } Gcc emits the warning Dereferencing void pointer. I understand the…
Bilow
  • 2,194
  • 1
  • 19
  • 34
4
votes
2 answers

How to get the offset of a nested struct member in C?

One solution to print the offset of the checksum field in the info struct, is to use the macros typeof and offsetof: #include #include #include typedef struct { struct { int a; } something; struct…
nowox
  • 25,978
  • 39
  • 143
  • 293
4
votes
1 answer

Using offsetof with a float in c

the code works fine for an int but when I want to use a float it fails unless I cast the structure as a character pointer. Here's what it looks like: struct test { float a; float b; }; void stuff(int offset, test* location); int main() { …
accasio
  • 185
  • 1
  • 3
  • 12