macro
offsetof
<cstddef>
Return member offset
This macro with functional form returns the offset value in bytes of member
member in the structure type
type.
The value returned is an unsigned integral value of type
size_t with the amount of bytes between the specified
member and the beginning of its structure.
Because of the extended functionality of structs in C++, in this language, the use of
offsetof is restricted to
"POD types", which for classes, more or less corresponds to the C concept of
struct (although non-derived classes with only public non-virtual member functions and with no constructor and/or destructor would also qualify as POD).
Parameters
- type
- A class type in which member is a valid member designator.
- member
- A member designator of class type.
Return value
A value of type
size_t with the offset value of
member in
type.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
/* offsetof example */
#include <stdio.h>
#include <stddef.h>
struct mystruct {
char singlechar;
char arraymember[10];
char anotherchar;
};
int main ()
{
printf ("offsetof(mystruct,singlechar) is %d\n",offsetof(mystruct,singlechar));
printf ("offsetof(mystruct,arraymember) is %d\n",offsetof(mystruct,arraymember));
printf ("offsetof(mystruct,anotherchar) is %d\n",offsetof(mystruct,anotherchar));
return 0;
}
|
Output:
offsetof(mystruct,singlechar) is 0
offsetof(mystruct,arraymember) is 1
offsetof(mystruct,anotherchar) is 11
|