简体   繁体   中英

Objective-C pointers as booleans

Going through some documentation on modifying CGImageRef data, I came across a strange example -- It went something along the lines of this pseudocode:

void *data = Allocate space for data;
if (data != NULL) Manipulate data;
if (data) Free data;

This got me wondering! What is the difference between the boolean operation if (data != NULL) and the boolean operation if (data) .

To be more specific, how do pointers behave in Objective C when they are treated as booleans? Attempting to google this, I only found myriads of questions relating to pointers- to -booleans, as opposed to pointers being evaluated as booleans.

They're exactly the same. Non-zero values of any type are interpreted as "true" in C, and by extension in Objective-C. C doesn't even have a boolean type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM