Pointer arithmetic hack

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


More tips

Help your fellow programmers! Add a tip!