首页 > 编程语言 > 详细

C++ 指针引用

时间:2016-06-15 10:35:47      阅读:265      评论:0      收藏:0      [点我收藏+]
//指针引用
#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\n", ERRO_MSG);
        return ERRO_MSG;
    }
    Teacher* ptemp = (Teacher*)malloc(sizeof(Teacher));
    if (ptemp == NULL)
    {
        ERRO_MSG = 2;
        printf("内存分配失败! erro msg:%d\n", 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\n", 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失败!\n");
    //}
    //printf("教师A的年龄是%d\n",p->age);
    ret = InitB(p);
    if (ret != 0)
    {
        printf("初始化Teacher失败!\n");
    }
    printf("教师B的年龄是%d\n", p->age);
    system("pause");
}

 

C++ 指针引用

原文:http://www.cnblogs.com/zhanggaofeng/p/5586470.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!