NULL

Sometimes you need a pointer to nothing. That is, you have a variable that can hold an address, and you want to store something in it that makes it explicit that the variable is not set to anything. We use NULL for this:

f​l​o​a​t​ ​*​m​y​P​o​i​n​t​e​r​;​
/​/​ ​S​e​t​ ​m​y​P​o​i​n​t​e​r​ ​t​o​ ​N​U​L​L​ ​f​o​r​ ​n​o​w​,​ ​I​'​l​l​ ​s​t​o​r​e​ ​a​ ​p​o​i​n​t​e​r​ ​t​h​e​r​e​
/​/​ ​l​a​t​e​r​ ​i​n​ ​t​h​e​ ​p​r​o​g​r​a​m​
m​y​P​o​i​n​t​e​r​ ​=​ ​N​U​L​L​;​

What is NULL? Remember that an address is just a number. NULL is zero. This is very handy in if statements:

f​l​o​a​t​ ​*​m​y​P​o​i​n​t​e​r​;​
.​.​.​
/​/​ ​H​a​s​ ​m​y​P​o​i​n​t​e​r​ ​b​e​e​n​ ​s​e​t​?​
i​f​ ​(​m​y​P​o​i​n​t​e​r​)​ ​{​
 ​ ​ ​ ​/​/​ ​m​y​P​o​i​n​t​e​r​ ​i​s​ ​n​o​t​ ​N​U​L​L​
 ​ ​ ​ ​.​.​.​d​o​ ​s​o​m​e​t​h​i​n​g​ ​w​i​t​h​ ​t​h​e​ ​d​a​t​a​ ​a​t​ ​m​y​P​o​i​n​t​e​r​.​.​.​
}​ ​e​l​s​e​ ​{​
 ​ ​ ​ ​/​/​ ​m​y​P​o​i​n​t​e​r​ ​i​s​ ​N​U​L​L​
}​

Later, when we discuss pointers to objects, we will use nil instead of NULL. They are equivalent, but Objective-C programmers use nil to mean the address where no object lives.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset