Pointer arithmetic for structs

后端 未结 5 2230
鱼传尺愫
鱼传尺愫 2021-02-07 08:02

Given a struct definition that contains one double and three int variables (4 variables in all), if p is a pointer to this struct with a value 0x1000, what value does p++ have?<

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-07 08:53

    Pointer arithmetic is done in units of the size of the pointer type.

    So if you do p++ on a pointer to your struct, p will advance by sizeof *p bytes. i.e. just ask your compiler for how big your struct is with the sizeof operator.

提交回复
热议问题