C - Find the size of structure

后端 未结 7 860
隐瞒了意图╮
隐瞒了意图╮ 2021-02-10 01:39

I was asked this as interview question. Couldn\'t answer.

Write a C program to find size of structure without using the sizeof operator.

相关标签:
7条回答
  • 2021-02-10 02:05

    Here is another approach.... no need to create any instance of structure.

    struct  XYZ{
        int x;
        float y;
        char z;
    };
    
    int main(){
        int sz = (int) (((struct XYZ *)0) + 1);
        printf("%d",sz);
        return 0;
    }
    

    How does it work?

    ((struct XYZ *)0) + 1 = zero + size of structure
                          = size of structure
    
    0 讨论(0)
提交回复
热议问题