zoukankan      html  css  js  c++  java
  • C指针

    void int_point();
    void One_dimensionalArray();
    void Two_dimensionalArray();
    int main()
    {
        int_point();
        One_dimensionalArray();
        Two_dimensionalArray();
    }
    //整形指针
    void int_point()
    {
        int *p;
        int a = 1;
        p = &a;
        //p1表示存放a的地址
        cout << p;
        cout << "
    ";
        // *p1表示取出地址p1里存放的数
        cout << *p;
        cout << "
    ";
    }
    //一维数组指针
    void One_dimensionalArray()
    {
        int *p;
        int arr[5] = { 1,2,3,4,5 };
        //p表示数组arr的首地址
        p = arr;
        cout << p;
        cout << "
    ";
        //也可以这样写,与p=arr是等价的
        p = &arr[0];
        cout << p;
        cout << "
    ";
        //用指针访问数组元素
        /*
            假设arr起始地址未1000;
            p(地址)           1000    1001    1002    1003    1004
            i               0       1       2       3       4
            *(p+i)(元素)  1       2       3       4       5
            数组的存放在内存中是连续的,所以第i个元素就是*(p+i).
        */
        for (int i = 0; i < 5; i++)
            cout << *(p + i);
        cout << "
    ";
    }
    //二维数组指针
    void Two_dimensionalArray()
    {
        int(*p)[5];//指针数组:*p指向一个地址中的内容,共有5个*p,即5个首地址
        int arr[5][5] = { 0 };//初始化数组;
        p = arr;
        cout << p;//p指向arr的首地址
        cout << "
    ";
        cout << p[0];//等价于p=arr;表示arr的首地址
        cout << "
    ";
        /*
            访问数组元素:
            二维数组在内存中的存储结构
            i=0时(假设arr的首地址为1001)
            j           0       1       2       3       4
            p[j]        1001    1002    1003    1004    1005
            (*p)[j]     num1    num2    num3    num4    num5
            i=1时
            j           0       1       2       3       4
            (p+1)[j]    1006    1007    1008    1009    1010
            (*(p+1))[j] num6    num7    num8    num9    num10
            .
            .
            .
            所以第i行的首地址和第i+1行的首地址差值为5;
        */
        for (int i = 0; i < 5; i++)
            for (int j = 0; j < 5; j++)
                cin >> (*(p + i))[j];//表示第i行第j列的元素(第i行首地址为p+i,第j行首地址为(p+i)[0])
        for (int i = 0; i < 5; i++)
            for (int j = 0; j < 5; j++)
    
    
    }
    
  • 相关阅读:
    twisted与websocket
    【算法竞赛 数学】拉格朗日插值法
    Event and Delegate
    Lambda 表达式 问题
    Singleton 模式
    COM Interop 简介
    outlook2007阻止附件的问题 http://hi.baidu.com/simplejoy/blog/item/53693897bd16046554fb9631.html
    Lnk1202 http://www.codeguru.com/forum/archive/index.php/t386908.html
    error LNK2001: unresolved external symbol
    Delegate 示例
  • 原文地址:https://www.cnblogs.com/cnsec/p/13286839.html
Copyright © 2011-2022 走看看