How to write a sizeof function using a macro function?

Questions by mani2881   answers by mani2881

Showing Answers 1 - 12 of 12 Answers

G. Siva Prakash Reddy

  • Oct 17th, 2007
 

Hi!

 #define size_of(data) ( (char *)(&data +1) - (char *)(&data))

    with Regards,

G. Siva Prakash Reddy.

  Was this answer useful?  Yes

Hi Siva,

     This would be the correct, 
     
#include<stdio.h>

#define mySizeof(x)(mySizeof1(x))

#define mySizeof1(x){__typeof__(x) tmp ;(char*)(&tmp + 1) - (char*)(&tmp);}

main()

{

int a;

printf("size of int %dn", mySizeof(int));

printf("size of a %dn", mySizeof(a));

}

Thanks for post,
Mani

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions