Forum

Number Of Page View:100,000+/Months
LOGIN |New User

Reply

How do you define offsetof() Macro?
ANSI C introduce a new macro called offsetof().This is use to determine the offset of a member in a structure.
  Using offsetof() we can easy to compute the amount of storage used by individual members of a structure.
Example of offsetof() macro are given below:

#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

struct foo {
  char *bar;
  int number;
};
// Print a foo the hard way
void printFoo(void *foo) {
   printf("foo->bar: %s
", *(char **) (foo +   offsetof(struct foo, bar)));
   printf("foo->number: %d
", *(int *)(foo + offsetof(struct foo, number)));
}
int main() 
{
   struct foo foo = {"A foo", 18};
   printFoo(&foo);
}
Posted By: Name:Rajesh Kr URL: How do you define offsetof() Macro?