標籤:
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=0x7fff5fbff79820 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=0x7fff5fbff79c27 *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=0x7fff5fbff79c29 }30 return 0;31 }
結果:
1 2015-05-09 20:42:11.593 OCPointerTypeParameter[562:21474] a=4, b=5; &a=0x7fff5fbff79c, &b=0x7fff5fbff7982 2015-05-09 20:42:11.594 OCPointerTypeParameter[562:21474] pointerTypeParameterTest(&a, b)=133 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=0x7fff5fbff79c5 2015-05-09 20:42:11.595 OCPointerTypeParameter[562:21474] c=8, a=8; &c=0x7fff5fbff790, c=0x7fff5fbff79c, &a=0x7fff5fbff79c
Objective-C文法之指標型參數