What exactly does the C Structure Dot Operator Do (Lower Level Perspective)?

前端 未结 4 2757
南笙
南笙 2021-02-20 16:42

I have a question regarding structs in C. So when you create a struct, you are essentially defining the framework of a block of memory. Thus when you create an instance of a str

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-20 17:09

    The dot operator simply selects the member.

    Since the compiler has information about the type (and consequently size) of the member (all members, actually), it knows the offset of the member from the start of the struct and can generate appropriate instructions. It may generate a base+offset access, but it also may access the member directly (or even have it cached in a register). The compiler has all those options since it has all the necessary information at compile time.

    If it hasn't, like for incomplete types, you'll get a compile-time error.

提交回复
热议问题