Monday, February 23, 2009

Pointer arithmetic hack

Here's a way to find the size of a struct without using the sizeof operator that also demonstrates how pointer arithmetic works.

struct point
{
int x ;
int y ;
}
struct point pt={0 , 0 };

point *ppt= &pt;
unsigned char *p1,*p2;
p1= (unsigned char*)ppt;
p2= (unsigned char*)++ppt;

printf("%d",p2-p1);

Author : Vivek Ahlawat

0 nhận xét:

Post a Comment