C Pointer Arithmetic sizeof(struct)

前端 未结 4 487
既然无缘
既然无缘 2021-01-05 07:19

Here is the code in question

#include 

struct test {
    unsigned char t;
    unsigned short u;
    unsigned char v;
};


int main ()
{
    s         


        
4条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-05 07:37

    You have a typed pointer.

    So when you increment it my 1 (i.e. a + 1) it means a + sizeof(type).

    So a + sizeof(type) = a + sizeof(type) * sizeof(type) = a + 6 * 6 (in your case as sizeof(test) = 6)

    That's where you are getting 0x24 or 36 from.

提交回复
热议问题