c语言中的结构体指针类型的cast

1.我们在c语言中会经常碰到强制类型转换。

在这,我介绍一种结构pointer类型转换,但是有前提(有点类似于c++中的继承中的子父对象的cast)。

简单的介绍一下:

 首先我们要知道一个结构的指针,并且 在这个结构体中,第一个结构成员必须也是一个结构体(最好是结构体类型).

那么我们可以这个结构体指针转换为指向这个结构体中第一个成员结构体的指针。

直接看代码:

************************************
 /*              struct transform for struct point                                                                                           
  2  *              author lkk
  3  *              time  2015-5-2
  4  *              inclcude a struct point
  5  */
  6 /*
  7  *  first a main struct vx_image( the key to include sub-struct) point
  8  *  we need transform the struct type to place first in the main struct
  9  *  some other struct 
 10  *
 11  */
 12  
 13 #include <stdio.h>
 14 // define vx_ref
 15 typedef struct _vx_ref{
 16         int a;
 17 }vx_ref_t;
 18 typedef struct _vx_ref *vx_ref;
 19 //define vx_scale
 20 typedef struct _vx_scale{
 21         int ab;
 22 }vx_scale_t;
 23 typedef struct _vx_scale *vx_scale;
 24 //define vx_image include the two struct vx_ref vx_scale
 25 typedef struct _vx_image {
 26         vx_ref_t ab;
 27         vx_scale_t ac;
 28         int b;
 29 }vx_image_t;
 30 typedef struct _vx_image *vx_image;
 31 //the main
 32 void main()
 33 {
 34         vx_image a;  //define a point to vx_image pointer
 35         a->ab.a = 1;// put to assignment of  sub_struct 
 36         a->ac.ab = 2;
 37         printf("********the old value*********\n");
 38         printf("the main struct value is %d  %d:\n",a->ab.a,a->ac.ab);
 39         printf("the transform first structure\n");
 40         vx_ref p = (vx_ref)a;// make the main struct pointer point to sub_strcut
 41 //      printf("the transform second structure\n");
 42         printf("the is %d\n",p->a);//output
 43 //      printf("we try\n");
 44 //      vx_scale q = (vx_scale)a;
 45 //      printf("the is %d ",a->ab);//output
 46  
 47 }
 48 // the conclusion 
 49 //in the main struct include some sub_strcut 
 50 //we can use first sub_struct pointer to cast(强制转换) the main structure pointer
 51 // example : sub_strcut pointer = (sub_struct) (the main structure pointer)                                                                 
 52 //so we get sub_struct pointer.
 53 //only by first point

  运行结果:

********the old value*********
the main struct value is 1 2:
the transform first structure
the is 1

刚好和自己的想法是一样的。

转载于:https://www.cnblogs.com/lkkandsyf/p/4472547.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【为什么还需要学习C++?】 你是否接触很多语言,但从来没有了解过编程语言的本质?你是否想成为一名资深开发人员,想开发别人做不了的高性能程序?你是否经常想要窥探大型企业级开发工程的思路,但苦于没有基础只能望洋兴叹? 那么C++就是你个人能力提升,职业之路进阶的不二之选。【课程特色】 1.课程共19大章节,239课时内容,涵盖数据结构、函数、类、指针、标准库全部知识体系。2.带你从知识与思想的层面从0构建C++知识框架,分析大型项目实践思路,为你打下坚实的基础。3.李宁老师结合4大国外顶级C++著作的精华为大家推出的《征服C++11》课程。【学完后我将达到什么水平?】 1.对C++的各个知识能够熟练配置、开发、部署;2.吊打一切关于C++的笔试面试题;3.面向物联网的“嵌入式”和面向大型化的“分布式”开发,掌握职业钥匙,把握行业先机。【面向人群】 1.希望一站式快速入门的C++初学者; 2.希望快速学习 C++、掌握编程要义、修炼内功的开发者; 3.有志于挑战更高级的开发项目,成为资深开发的工程师。 【课程设计】 本课程包含3大模块基础篇本篇主要讲解c++的基础概念,包含数据类型、运算符等基本语法,数组、指针、字符串等基本词法,循环、函数、类等基本句法等。进阶篇本篇主要讲解编程常用的一些技能,包含类的高级技术、类的继承、编译链接和命名空间等。提升篇:本篇可以帮助学员更加高效的进行c++开发,其包含类型转换、文件操作、异常处理、代码重用等内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值