Objective C 的新手。尝试理解 Objective C 中的指针。
我的代码
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *msg = @"Hello Bhai";
NSLog(@" address is %i for msg", msg);
NSLog(@" which is same address %i for Hello Bhai", @"Hello Bhai");
NSLog(@" which is different address %i for Hello mere Bhai", @"Hello mere Bhai");
NSLog(@" value is %@ at the address for msg", msg);
[pool drain];
return 0;
}
输出是
[Session started at 2011-04-26 16:31:11 -0400.]
2011-04-26 16:31:11.656 BasicObjC[1523:10b] address is 8240 for msg
2011-04-26 16:31:11.658 BasicObjC[1523:10b] which is same address 8240 for Hello Bhai
2011-04-26 16:31:11.659 BasicObjC[1523:10b] which is different address 8288 for Hello mere Bhai
2011-04-26 16:31:11.660 BasicObjC[1523:10b] value is Hello Bhai at the address for msg
这是否意味着 Objective C 中的 @ 与 C 中的 & 相同
最佳答案
@ 用于表示字符串是 NSString 文字,因此您可以将其分配给 NSString
对象。
如果你想打印一些地址,那么你应该使用 %p 格式说明符,而不是 %i,例如
NSLog(@" address is %p for msg", msg);
关于objective-c - Objective-C 中的 @ 运算符是否与 C 中的 & 运算符相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5796244/