Pointer arithmetic hack

This tip submitted by Vivek Ahlawat on 2005-03-07 02:09:20. It has been viewed 41177 times.
Rating of 5.5 with 173 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!