use*_*102 2 pointers objective-c nsstring
新目标C.试图理解目标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;
}
Run Code Online (Sandbox Code Playgroud)
输出是
[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
Run Code Online (Sandbox Code Playgroud)
这是否意味着Objective C中的@与C中的&相同
@用于表示该字符串是NSString文字,因此您可以将其分配给NSString
对象.
如果你想打印一些地址,那么你应该使用%p格式说明符,而不是%i,例如
NSLog(@" address is %p for msg", msg);
Run Code Online (Sandbox Code Playgroud)