#import <Foundation/Foundation.h>
BOOL areIntsDifferent(int thing1, int thing2) {
if (thing1 == thing2) {
return (NO);
} else {
return (YES);
}
}
NSString *boolString (BOOL yesNo) {
if (yesNo == NO) {
return(@"NO");
} else {
return(@"YES");
}
}
int main (int argc, const char * argv[]) {
BOOL areTheyDifferent;
areTheyDifferent = areIntsDifferent (5,5);
NSLog(@"are %d and %d different? %@", 5, 5, boolString(areTheyDifferent));
areTheyDifferent = areIntsDifferent (23,42);
NSLog(@"are %d and %d different? %@", 23, 42, boolString(areTheyDifferent));
return (0);
}
这是整个程序。这不是非常复杂,但它说明了我经常遇到的一个常见问题 - 理解指针。在这种情况下,boolString函数的返回值是一个指针,因为没有分配内存来存放一个字符串?如果是这样,为什么不是areInts不同的返回值指向BOOL值的指针?我可以重写这个程序,以便boolString的返回值是一个字符串,而不是一个指向字符串的指针吗?如果是这样,如何?我试图消除*,但然后我得到一个编译器错误。基本Objective-C指针问题
http://stackoverflow.com/questions/3951484/basic-objective-c-pointer-question重复? – 2010-10-17 01:01:49
@Joe另外一个人得到了最近的投票,但感谢你指出了它。 :) – 2010-10-17 01:20:02