模仿sizeof的实现方法

#include <stdio.h>

 

#define sizeof_value( value )/
    ( ( char* )(&value+1) - ( char* )&value  )

 

#define sizeof_type(T) ( ( char* )( ( T* )0 + 1 ) )

 

int main(int argc, char* argv[])
{
    char stop[20];

    int size_int = sizeof_type( int );
    int size_char_def = sizeof_value( stop );
    struct s_t
    {
        char a;
        int b;
    };
    struct s_t t;
    printf( "%d %d %d %d ", sizeof_type( char ), sizeof_type( int ),  sizeof_type( double ),  sizeof_value( t ) );
    printf( "%d ",  size_int );
    printf( "%d",  size_char_def );

    gets( stop );
    return 0;
}

你可能感兴趣的:(struct,include)