结构成员地址和成员指针

#include<iostream>
using namespace std;
struct date{
    int year;
    int month;
    int day;
    void print(){
        cout << year << "-" << month << "-" << day << endl;
    }
};

int main()
{
    cout << main << endl;//输出1
    //取结构成员,使用::号
    cout << &date::year << endl;//1
    cout << &date::month << endl;//1
    cout << &date::day << endl;//1
    //c++中函数地址和成员地址不允许输出,非要输出就输出都是1
    
    //--偏移地址--------------------------------------------------
    union data{//现在我构造一个联合体,查看偏移地址是多少
        int n;
        int date::*p;//date结构成员指针
    };
    data idata;

    idata.p = &date::year;
    cout << "year=" << idata.n << endl;//输出0
    idata.p = &date::month;
    cout << "month=" << idata.n <<endl;//输出4
    idata.p = &date::day;
    cout << "day=" << idata.n <<endl;//输出8
    //date地址+0是year地址,+4是month地址,+8是day地址,048叫偏移地址
    
    //---------------------------------------------------------------
    struct date d = {2021,10,1};//现在定义一个结构体变量d
    cout << &d << endl;
    cout << &d.year << endl;
    cout << &d.month << endl;
    cout << &d.day << endl;
    d.print();

    int date::*q;//定义一个结构成员指针
    q =&date::year;//q指向year
    cout << d.*q <<endl;//

    void (date::*pf)()=&date::print;//定义一个执行结构成员函数的指针,这里取他的偏移地址,真实地址要看date的实例化地址,加上偏移量
    (d.*pf)();

    return 0;
}

成员地址不依赖于某个结构对象,

结构成员地址和成员指针通过相对偏移量访问机构的里的某成员

#include<iostream>
using namespace std;
struct date{
    int year;
    int month;
    int day;
};
void showMember(date a[],int size,int date::*p)
{
    int i = 0;
    while(i<size)
        cout << a[i++].*p << endl;
}
int main()
{
    date a[3]={{2001,2,3},{2004,5,6},{2007,8,9}};
    showMember(a,3,&date::year);
    showMember(a,3,&date::month);
    showMember(a,3,&date::day);
    return 0;
}
#include<iostream>
using namespace std;
class date{//把class换成struct一样效果
    public:
        int year;
        int month;
        int day;
};

void print(struct date* that){
    cout << that->year << "-" << that->month << "-" << that->day << endl;
}

int main()
{
    class date d = {2021,10,1};//把class date d = {2021,10,1};换成struct date d = {2021,10,1};一样效果
    cout << &d << endl;
    cout << &d.year << endl;
    cout << &d.month << endl;
    cout << &d.day << endl;
 
    class date* that;//把class换成struct一样效果
    that = &d;
    cout << that->year <<endl;//

    print(that);

    return 0;
}
#include<stdio.h>
#include<string.h>
 
struct student
{
    int No;
    char Name[20];
};
void Init(struct student* const this,int No,char Name[])
{
    this->No=No;
    strcpy(this->Name,Name);
}
 
void setName(struct student* const this,char Name[])
{
    strcpy(this->Name,Name);
}
 
char* getName(struct student* const this)
{
    return this->Name;
}
 
void show(struct student* const this)
{
    printf("%d %s \n",this->No,this->Name);
}
 
int main()
{
    struct student s1;
    Init(&s1,23,"zhangsan");
    show(&s1);
    setName(&s1,"lisi");
    show(&s1);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值