블로그 이미지
kalstein

여러가지 프로그래밍 관련이나...신변잡기적인 글들을 남기는 블로그입니다. 지식은 나누는만큼 강력해집니다 ^^

Rss feed Tistory
Programming 2010. 6. 29. 23:08

C Structure에서의 this pointer 찾기.

C언어에서 this라니 묘하긴하지만.

struct A
{
   int a, b;
};
가 되어있을때, A 구조체의 b 포인터에서 A 구조체의 포인터를 거슬러 올라가서 구하는? 뭐 그런것.

#define container_of(ptr, type, member) ({   \
const typeof( ((type *)0->member ) * __mptr = (ptr);   \
(type *)( (char *) __mptr - offsetof(type, member) ); } )

길기도 하지 -_-;;;
A structure로 예를 들어보자면, 코드는 다음과 같이 활용될수있다.

void foo (int * b)
{
struct A * p;
p = container_of(b, struct A, b);
}

좀 복잡하긴함...^^;;
,
TOTAL TODAY