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

    //指针引用
    #include<iostream>
    
    using namespace std;
    
    struct Teacher{
        char name[30];
        int age;
    };
    
    int InitA(Teacher **pout/*out*/){
        int ERRO_MSG = 0;
        if (pout==NULL)
        {
            ERRO_MSG = 1;
            printf("pout==NULL erro msg:%d
    ", ERRO_MSG);
            return ERRO_MSG;
        }
        Teacher* ptemp = (Teacher*)malloc(sizeof(Teacher));
        if (ptemp == NULL)
        {
            ERRO_MSG = 2;
            printf("内存分配失败! erro msg:%d
    ", ERRO_MSG);
            return ERRO_MSG;
        }
        ptemp->age = 22;
        *pout = ptemp;
        return ERRO_MSG;
    }
    
    //指针引用
    int InitB(Teacher* &pout){
        int ERRO_MSG = 0;
        pout = (Teacher*)malloc(sizeof(Teacher));
        if (pout == NULL)
        {
            ERRO_MSG = 2;
            printf("内存分配失败! erro msg:%d
    ", ERRO_MSG);
            return ERRO_MSG;
        }
        pout->age = 33;
        return ERRO_MSG;
    }
    
    void main(){
        Teacher *p = NULL;
        int ret = 0;
        //ret=InitA(&p);
        //if (ret!=0)
        //{
        //    printf("初始化Teacher失败!
    ");
        //}
        //printf("教师A的年龄是%d
    ",p->age);
        ret = InitB(p);
        if (ret != 0)
        {
            printf("初始化Teacher失败!
    ");
        }
        printf("教师B的年龄是%d
    ", p->age);
        system("pause");
    }
  • 相关阅读:
    集训队作业2018人类的本质
    推式子小技巧
    [Codeforces671D]Roads in Yusland
    线性规划的对偶问题
    数学虐哭空巢老人记
    Voronoi图与Delaunay三角剖分
    [ZJOI2018]保镖
    [SPOJ2939]Qtree5
    数据结构虐哭空巢老人记
    [CTSC2006]歌唱王国
  • 原文地址:https://www.cnblogs.com/zhanggaofeng/p/5586470.html
Copyright © 2011-2022 走看看