Friday, 1 February 2013

SIZEOF implementation in C

// Program to implement sizeof operator
#include <stdio.h> #define my_sizeof(type) (char *)(&type + 1)-(char *)(&type)
int main(){     char x;     printf("%d %d \n",(char *)(&x + 1), (char *)(&x));     printf("%d", my_sizeof(x));     getchar();     return 0; }

2 comments:

  1. Can u please explain the program?? esp the macro part

    ReplyDelete
    Replies
    1. Macro substitues the line and does the pointer arithematic for obtaining the size. Key point to know here is the difference between the normal arithematic operation and pointer arithematic....

      Delete