Objective-C文法之指標型參數

來源:互聯網
上載者:User

標籤:

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文法之指標型參數

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.