指位器的定義、功用及使用方法

我是從Basic入門的,在看到C語言時,令我一直參不透的一個東西就是指位器。請問指位器的定義、功用及用法為何?可以的話,請順便舉例說明?

2 Answers

Rating
  • Anonymous
    2 decades ago
    Favorite Answer

    指位器是pointer嗎?

    pointer就是一個指向地址的記憶體

    例如有一個variable : int Num(整數變數), 它的值是12, 地址是 0x12ff43

    pointer的作用就是指向地址 0x12ff43, 即指向變數Num了

    假設一個int pointer : int *ptr(指向整數的指位器), point to Num : ptr=&Num

    要取得pointer的地址 : ptr =>傳回 0x12ff43

    要取得pointer的地址內的值 : *ptr =>傳回 12

    作用是直接修改地址內的值, func pass by ref

    亦可指向一連續位置方便存取, array pointer

    給你一個實例

    void add_3_without_return_value(int *pass_by_ref_equalto_pointer)

    {

    *pass_by_ref_equalto_pointer+=3;

    }

    main()

    {

    int Num=12; //declare integer Num and default value =12

    int *ptr; //declare integer pointer ptr

    ptr=&Num; //pass the address of variable Num to pointer

    cout << "ptr add. = " << ptr << ", as smae as Num add. = " << &Num << endl

    << "Num value = " << Num << ", as same as ptr value = " << *ptr << endl;

    add_3_without_return_value(ptr);

    cout << "value of ptr after change = " << *ptr << ", value of Num is also changed = "

    << Num << endl;

    }

Still have questions? Get your answers by asking now.