The "offsetof" function in c is useful in determining the offset of members with in a structure or union.
It is defined in the header file "stddef.h".
The syntax is
Arguments:
Example :
Let say we have a structure foo
The structure "foo" has a integer member which generally would of 4 bytes on a 32 bit system and a chat member which would be one byte.
Thus the offset of the member "ch" in the structure "foo" would be the number of bytes occupied by "int" which is "4".
Here is a example code which uses "offsetof" to find the offset of ch in the structre foo.
Compile the code and execute it
Try changing adding new members to the structure foo and see how the result changes depending on the members added.
Try with the structure :
The offset is the size of the array which is 4 X 10 = 40.
It is defined in the header file "stddef.h".
The syntax is
Arguments:
Example :
Let say we have a structure foo
The structure "foo" has a integer member which generally would of 4 bytes on a 32 bit system and a chat member which would be one byte.
Thus the offset of the member "ch" in the structure "foo" would be the number of bytes occupied by "int" which is "4".
Here is a example code which uses "offsetof" to find the offset of ch in the structre foo.
Compile the code and execute it
Try changing adding new members to the structure foo and see how the result changes depending on the members added.
Try with the structure :
The offset is the size of the array which is 4 X 10 = 40.
No comments:
Post a Comment