指针 取地址& 解引用 *


#include<iostream>
using namespace std;

int main()
{
    int a = 123;
    float b = 3.14;
    char c = 'C';
    unsigned long d = 19880808;
    string e = "i love fishc";

    cout << "a is " << a << endl;
    cout << "b is " << b << endl;
    cout << "c is " << c << endl;
    cout << "d is " << d << endl;
    cout << "e is " << e << endl;

    int *ap = &a;
    float *bp = &b; // * 作用1:声明指针变量,这里是装地址的。
    char *cp = &c;
    unsigned long *dp = &d;
    string *ep = &e;
  
    *ap = 456;     // * 解引用
    *bp = 4.13;
    *cp = 'e';
    *dp = 19990909;
    *ep = "cover me";

    cout << "a is " << a << endl;
    cout << "b is " << b << endl;
    cout << "c is " << c << endl;
    cout << "d is " << d << endl;
    cout << "e is " << e << endl;

}
变量的前面表示取变量地址赋值给指针, 如:int a = 0; int *pa = &a;
类型后面表示引用,引用即变量的替身。 int a = 0; int &ref = a;操作ref就跟操作a是一样的
/* vim: set ts=4 sw=4 sts=4 tw=100 */

 

posted on 2015-03-18 22:48  雨渐渐  阅读(428)  评论(0编辑  收藏  举报

导航