#include <stdio.h>

//父结构体
struct father
{
int f1;
int f2;
};

//子结构体
struct son
{
//子结构体里定义一个父结构体变量,必须放在子结构体里的第一位
struct father fn;
//子结构体的扩展变量
int s1;
int s2;
};

void test(struct son *t)
{
//将子结构体指针强制转换成父结构体指针
struct father *f = (struct father *)t;
//打印原始值
printf("f->f1 = %d\n",f->f1);
printf("f->f2 = %d\n",f->f2);
//修改原始值
f->f1 = 30;
f->f2 = 40;
}

int main(void)
{
struct son s;
s.fn.f1 = 10;
s.fn.f2 = 20;

test(&s);
//打印修改后的值
printf("s.fn.f1 = %d\n",s.fn.f1);
printf("s.fn.f2 = %d\n",s.fn.f2);

return 0;
}

相关文章:

  • 2022-02-01
  • 2022-12-23
  • 2022-01-17
  • 2022-02-25
  • 2021-10-10
  • 2022-12-23
  • 2022-12-23
  • 2021-05-01
猜你喜欢
  • 2022-12-23
  • 2021-05-22
  • 2022-12-23
  • 2021-08-23
  • 2021-11-30
  • 2022-12-23
  • 2022-01-14
相关资源
相似解决方案