Avoid dereferencing NULL

Sometimes a function can supply many values by reference, but you may only care about some of them. How do you avoid declaring these variables and passing their addresses when you’re not going to use them anyway? Typically, you pass NULL as an address to tell the function I don’t need this particular value.

This means that you should always check to make sure the pointers are non-NULL before you dereference them. Add these checks in cartesianToPolar():

v​o​i​d​ ​c​a​r​t​e​s​i​a​n​T​o​P​o​l​a​r​(​f​l​o​a​t​ ​x​,​ ​f​l​o​a​t​ ​y​,​ ​ ​d​o​u​b​l​e​ ​*​r​P​t​r​,​ ​d​o​u​b​l​e​ ​*​t​h​e​t​a​P​t​r​)​
{​
 ​ ​ ​ ​/​/​ ​I​s​ ​r​P​t​r​ ​n​o​n​-​N​U​L​L​?​
 ​ ​ ​ ​i​f​ ​(​r​P​t​r​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​S​t​o​r​e​ ​t​h​e​ ​r​a​d​i​u​s​ ​i​n​ ​t​h​e​ ​s​u​p​p​l​i​e​d​ ​a​d​d​r​e​s​s​
 ​ ​ ​ ​ ​ ​ ​ ​*​r​P​t​r​ ​=​ ​s​q​r​t​(​x​ ​*​ ​x​ ​+​ ​y​ ​*​ ​y​)​;​
 ​ ​ ​ ​}​

 ​ ​ ​ ​/​/​ ​I​s​ ​t​h​e​t​a​P​t​r​ ​N​U​L​L​?​
 ​ ​ ​ ​i​f​ ​(​!​t​h​e​t​a​P​t​r​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​S​k​i​p​ ​t​h​e​ ​r​e​s​t​ ​o​f​ ​t​h​e​ ​f​u​n​c​t​i​o​n​
 ​ ​ ​ ​ ​ ​ ​ ​r​e​t​u​r​n​;​
 ​ ​ ​ ​}​

 ​ ​ ​ ​/​/​ ​C​a​l​c​u​l​a​t​e​ ​t​h​e​t​a​
 ​ ​ ​ ​f​l​o​a​t​ ​t​h​e​t​a​;​
 ​ ​ ​ ​i​f​ ​(​x​ ​=​=​ ​0​.​0​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​i​f​ ​(​y​ ​=​=​ ​0​.​0​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​t​h​e​t​a​ ​=​ ​0​.​0​;​ ​ ​ ​ ​/​/​ ​t​e​c​h​n​i​c​a​l​l​y​ ​c​o​n​s​i​d​e​r​e​d​ ​u​n​d​e​f​i​n​e​d​
 ​ ​ ​ ​ ​ ​ ​ ​}​ ​e​l​s​e​ ​i​f​ ​(​y​ ​>​ ​0​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​t​h​e​t​a​ ​=​ ​M​_​P​I​_​2​;​
 ​ ​ ​ ​ ​ ​ ​ ​}​ ​e​l​s​e​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​t​h​e​t​a​ ​=​ ​-​ ​M​_​P​I​_​2​;​
 ​ ​ ​ ​ ​ ​ ​ ​}​
 ​ ​ ​ ​}​ ​e​l​s​e​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​.​.​.​
..................Content has been hidden....................

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