The nullptr and pointer arithmetic

前端 未结 4 590
执念已碎
执念已碎 2021-02-05 21:23

Considering the following code, is it safe to do pointer arithmetic on nullptr?

I assume adding any offsets to a nullptr results in another

4条回答
  •  北恋
    北恋 (楼主)
    2021-02-05 22:14

    is it safe to do pointer arithmetic on nullptr? 
    

    C++ defines two kind of operations on nullptr. For :

    float * x=nullptr;
    float * y=nullptr;
    
    1. x +/- 0 = x

    2. x-y=0 //note x and y have the same type

    You cannot make an assumption about what is not defined, so you shouldn't do it.

提交回复
热议问题