Dudu it works fine for variables.....not for data types.shows compiletime error...check this code.
#include<iostream> //#define size(T)(*((char*)&T+1-*(char*)&T)); #define SIZEOF(var) (size_t)(&var+1) - (size_t)(&var) using namespace std; int main() { char a; cout<<sizeof(char); //original sizeof .... it outputs 1 cout<<SIZEOF(char); cin.sync(); cin.get(); return 0; } by the way what that sync() do? On 5/31/10, sharad kumar <aryansmit3...@gmail.com> wrote: > #include<iostream> > //#define size(T)(*((char*)&T+1-*(char*)&T)); > #define SIZEOF(var) (size_t)(&var+1) - (size_t)(&var) > > using namespace std; > int main() > { > char a; > cout<<SIZEOF(a); > cin.sync(); > cin.get(); > return 0; > } > > > > On Mon, May 31, 2010 at 1:14 PM, debajyotisarma > <sarma.debajy...@gmail.com>wrote: > >> This is not about algorithms,but related to C programming. >> >> How to implement sizeof operator? >> >> macro for this >> #define my_sizeof(a) (char*)(&a+1)-(char*)&a >> >> this works fine of variables >> int a; >> printf("%d",my_sizeof(a)); //or even for user defined >> structures >> >> but it will not work for data types >> >> like >> >> printf("%d",my_sizeof(int)); >> >> so please get another solution. >> function will be preferable.not macro >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To post to this group, send email to algoge...@googlegroups.com. >> To unsubscribe from this group, send email to >> algogeeks+unsubscr...@googlegroups.com<algogeeks%2bunsubscr...@googlegroups.com> >> . >> For more options, visit this group at >> http://groups.google.com/group/algogeeks?hl=en. >> >> > > > -- > yezhu malai vaasa venkataramana Govinda Govinda > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algoge...@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.