main.m
1 #import <Foundation/Foundation.h> 2 /** 3 * 测试指针型参数和普通参数的区别 4 * 5 * @param a 指针型参数 6 * @param b 普通参数 7 * 8 * @return (指针型参数+2) + (普通参数+2) 9 */ 10 int pointerTypeParameterTest(int *a, int b) { 11 *a = *a + 2; //*a表示获取a变量指针(内存地址)所指向内存存储空间内的值 12 b = b + 2; 13 return *a+b; 14 } 15 int main(int argc, const char * argv[]) { 16 @autoreleasepool { 17 int a = 4; 18 int b = 5; 19 NSLog(@"a=%d, b=%d; &a=%p, &b=%p", a, b, &a, &b); //a=4, b=5; &a=0x7fff5fbff79c, &b=0x7fff5fbff798 20 NSLog(@"pointerTypeParameterTest(&a, b)=%d", pointerTypeParameterTest(&a, b)); //pointerTypeParameterTest(&a, b)=13;&a表示获取a变量的内存地址,b表示获取变量的值 21 NSLog(@"a=%d, b=%d; &a=%p, &b=%p, after the operation of pointerTypeParameterTest(&a, b)", a, b, &a, &b); //a=6, b=5; &a=0x7fff5fbff79c, &b=0x7fff5fbff798, after the operation of pointerTypeParameterTest(&a, b) 22 23 24 int *c; 25 c = &a; 26 NSLog(@"c=%d, a=%d; &c=%p, c=%p, &a=%p", *c, a, &c, c, &a); //c=6, a=6; &c=0x7fff5fbff790, c=0x7fff5fbff79c, &a=0x7fff5fbff79c 27 *c = 8; 28 NSLog(@"c=%d, a=%d; &c=%p, c=%p, &a=%p", *c, a, &c, c, &a); //c=8, a=8; &c=0x7fff5fbff790, c=0x7fff5fbff79c, &a=0x7fff5fbff79c 29 } 30 return 0; 31 }
结果:
1 2015-05-09 20:42:11.593 OCPointerTypeParameter[562:21474] a=4, b=5; &a=0x7fff5fbff79c, &b=0x7fff5fbff798 2 2015-05-09 20:42:11.594 OCPointerTypeParameter[562:21474] pointerTypeParameterTest(&a, b)=13 3 2015-05-09 20:42:11.594 OCPointerTypeParameter[562:21474] a=6, b=5; &a=0x7fff5fbff79c, &b=0x7fff5fbff798, after the operation of pointerTypeParameterTest(&a, b) 4 2015-05-09 20:42:11.594 OCPointerTypeParameter[562:21474] c=6, a=6; &c=0x7fff5fbff790, c=0x7fff5fbff79c, &a=0x7fff5fbff79c 5 2015-05-09 20:42:11.595 OCPointerTypeParameter[562:21474] c=8, a=8; &c=0x7fff5fbff790, c=0x7fff5fbff79c, &a=0x7fff5fbff79c
时间: 2024-10-14 07:08:26