Pointer arithmetic hack

This tip submitted by Vivek Ahlawat on 2005-03-07 02:09:20. It has been viewed 35211 times.
Rating of 6.4 with 110 votes



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);




More tips

Help your fellow programmers! Add a tip!