doubt in sizeof implementation

Posted by aks on Stack Overflow See other posts from Stack Overflow or by aks
Published on 2010-04-09T14:43:47Z Indexed on 2010/04/09 14:53 UTC
Read the original article Hit count: 284

Filed under:

Below is the program to find the size of a structure without using sizeof operator:

struct MyStruct
{
  int i;
  int j;
};

int main()
{
   struct MyStruct *p=0;
   int size = ((char*)(p+1))-((char*)p);
   printf("\nSIZE : [%d]\nSIZE : [%d]\n", size);
   return 0;
}

My doubt is:

Why is typecasting to char * required?

If I don't use the char* pointer, the output is 1 - WHY?

© Stack Overflow or respective owner

Related posts about c