Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 5 years have passed since last update.

構造体メンバのオフセット取得

Last updated at Posted at 2015-03-16

構造体メンバのアドレスのオフセットを取得


#include <stdio.h>
#include <stddef.h>   // offsetof()

struct x
{
  int a;
  char b;
};

size_t sz;

sz = 
/* offsetof(Data, Member) */
offsetof(struct x, a);

printf("%zu\n", sz);

sz = offsetof(struct x, b);
printf("%zu\n", sz);




#include <stdio.h>
 
struct Test
{
  char c;
  int n;
  double d;
};
 
int main(void)
{
  size_t sc = (size_t) &((struct Test*)0)-> c;
  size_t sn = (size_t) &((struct Test*)0)-> n;
  size_t sd = (size_t) &((struct Test*)0)-> d;
 
  printf("%d %d %d\n", sc, sn, sd);
	
  return 0;
}

0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?