tags:

views:

169

answers:

4

here is code

#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 of which is

offsetof(mystruct,singlechar) is 0
offsetof(mystruct,arraymember) is 1
offsetof(mystruct,anotherchar) is 11

i have read this documentation

http://www.cplusplus.com/reference/clibrary/cstddef/offsetof/

but i did not understand no not because of my english but i dont understand documenattion itself please can anybody give me a little example or tell me how it works? thanks

+2  A: 

Have you tried Wikipedia? Believe it or not, it has an informative article on the offsetof macro: http://en.wikipedia.org/wiki/Offsetof

offsetof is neither function nor procedure, but a preprocessor macro, which apparently expands to

#define offsetof(st, m) \
    ((size_t) ( (char *)&((st *)(0))->m - (char *)0 ))

As a possible use, Wikipedia mentions the implementation of generic datastructures in C.

A quick search in Google Code Search (http://www.google.com/codesearch?q=offsetof&amp;hl=en&amp;btnG=Search+Code) reveals that it is indeed used for this and other purposes in numerous projects.

Greg S
There is no requirement that it expands to that expression, and newer compilers may define it to a special form instead (GCC 4 uses __builtin_offsetof(st, m) for instance). You should always get offsetof from <stddef.h> rather than trying to define it yourself.
Zack
+3  A: 

If you have a pointer to the structure, the value returned by offsetof indicates how many bytes you would have to adjust the pointer to be a pointer to an individual element of the structure.

If you don't quite understand how this works, you would be best off avoiding it altogether. This is one of the ways that C++ allows you to shoot yourself in the foot.

Mark Ransom
And we know that with C++ `shooting yourself in the foot == blowing of your whole leg.`
torak
+1 for the warning.
Merlyn Morgan-Graham
+2  A: 

Having this structure

struct mystruct {
  char singlechar;
  char arraymember[10];
  char anotherchar;
};

The offsetoff function tells you the distance in bytes between 2 memory adresses inside the same structure.

You get this output:

offsetof(mystruct,singlechar) is 0
offsetof(mystruct,arraymember) is 1
offsetof(mystruct,anotherchar) is 11

Because char uses 1byte. Using ofssetof(structure_name,structure_element); calculates the offset from the first element of the structure to the "structure_eluement".

  • offsetof(mystruct,singlechar) is 0 Because "singlechar" is declared the first inside the structure.
  • offsetof(mystruct,arraymember) is 1 Because "arraymember" is declared the second inside the structure, and 1byte was previous declared (the "singlechar").
  • offsetof(mystruct,arraymember) is 11 Because there are 11bytes from the structure begining to "anotherchar" (1byte for singlechar and 10bytes for arraymember).

Hope that makes it clear...

Cristy
+2  A: 

offsetof tells you where in the memory allocation of the structure you will find a particular member.

The structure you defined takes up 12 bytes:

struct mystruct {
  char singlechar;
  char arraymember[10];
  char anotherchar;
};
  • byte 0: singlechar
  • byte 1: arraymember[0]
  • byte 2: arraymember[1]
  • byte 3: arraymember[2]
  • ...
  • byte 10: arraymember[9]
  • byte 11: anotherchar

This is why you get the output:

offsetof(mystruct,singlechar) is 0
offsetof(mystruct,arraymember) is 1
offsetof(mystruct,anotherchar) is 11

If you allocate an object of that type, and get a byte* to the start of the structure, you can uses offsetof to find out where each member is. If you use that pointer offset, and convert it back to the correct type, it will give you a pointer to the member.

mystruct s;
s.anotherchar = 5;
char* pBeginningOfS = (char*)(void*)(&s);
char* pAnotherChar = pBeginningOfS + offsetof(mystruct, anotherchar);
*pAnotherChar = 17;
printf("anotherchar is %d", s.anotherchar);
return 0;

This will output:

anotherchar is 17

The reason you can't assume that each member will be a specific offset from the beginning of the struct is complicated, and compiler dependent. If you must do something like this (which is really low-level stuff, and you should avoid unless you have to), then use a macro like offsetof, rather than trying to manually specify the offset yourself.

Merlyn Morgan-Graham