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
刚好和自己的想法是一样的。
c语言中的结构体指针类型的cast的更多相关文章
- C语言中全局结构体指针隐含的错误
前天在嵌入式系统上,调试一个数组的全局变量时,发现该变量一直会动态变化.深入分析, 才发现该全局结构体没有申请内存,而是用了一个指针.这种情况编译器是检查不出来的,在linux 上运行会挂掉,但是在裸 ...
- go语言之进阶篇结构体指针类型匿名字段
1.结构体指针类型匿名字段 示例: package main import "fmt" type Person struct { name string //名字 sex byte ...
- C语言中的结构体,结构体数组
C语言中的结构体是一个小难点,下面我们详细来讲一下:至于什么是结构体,结构体为什么会产生,我就不说了,原因很简单,但是要注意到是结构体也是连续存储的,但要注意的是结构体里面类型各异,所以必然会产生内存 ...
- C语言中的结构体
用户自己建立自己的结构体类型 1. 定义和使用结构体变量 (1).结构体的定义 C语言允许用户自己建立由不同类型数据组成的组合型的数据结构,它称为结构体. (2).声明一个结构体类型的一般形式为: ...
- Verilog缺少一个复合数据类型,如C语言中的结构体
https://mp.weixin.qq.com/s/_9UsgUQv-MfLe8nS938cfQ Verilog中的数据类型(Data Type)是分散的,缺少一个复合数据类型:把多个wire, r ...
- C语言中访问结构体成员时用‘.’和‘->’的区别
举个例子,定义了一个叫Student,别名为stu的结构类型,我们声明了一个结构体变量叫stu1,声明了一个结构体指针为stuP. typedef struct Student { char name ...
- C语言中的结构体和C++中的结构体以及C++中类的区别
c++中结构体可以定义一个函数 C中的结构体和C++中结构体的不同之处:在C中的结构体只能自定义数据类型,结构体中不允许有函数,而C++中的结构体可以加入成员函数. C++中的结构体和类的异同: 一. ...
- C语言中处理结构体的原理
汇编中有几种寻址方式,分别是直接寻址:(ds:[idata]).寄存器间接寻址(ds:[bx]).寄存器相对寻址(ds:[bx + idata].ds:[bx + si])基址变址寻址(ds:[bx ...
- 嵌入式-C语言:通过结构体指针操作结构体内容
#include<stdio.h> #include<string.h> struct Student { char name[32]; int age; int height ...
随机推荐
- ROW_NUMBER()/RANK()/DENSE_RANK()/ntile() over()
ROW_NUMBER()/RANK()/DENSE_RANK()/ntile() over() 今天女票问我SqlServer的四种排序,当场写了几句Sql让她了解,现把相关Sql放上来. 首先, ...
- Java虚拟机--字节码指令集
1. 字节码指令集简介: Java虚拟机的指令由一个字节长度的,代表着某种特定操作含义的操作码(Opcode)以及跟随其后的零至多个代表此操作所需参数的操作数(Operands)所构成.虚拟机中许多指 ...
- MongoDB学习笔记--基本命令
转自:http://www.cnblogs.com/xusir/archive/2012/12/24/2830957.html 数据库文件默认位置 /var/lib/mongodb 成功启动Mongo ...
- Gradient Descent 和 Stochastic Gradient Descent(随机梯度下降法)
Gradient Descent(Batch Gradient)也就是梯度下降法是一种常用的的寻找局域最小值的方法.其主要思想就是计算当前位置的梯度,取梯度反方向并结合合适步长使其向最小值移动.通过柯 ...
- 程序被送入后台后,向 iOS 借时间,完成长期任务-备
12.2.2. 方案 使用UIApplication的beginBackgroundTaskWithExpirationHandler: 实例方法.在你完成任务后,调用UIApplication的en ...
- 禁用与启用Button点击
//启用查询按钮 btnFpSelect.setClickable(true); //禁用查询按钮 btnFpSelect.setClickable(false);
- QT下资源使用和资源占用…(可以动态加载资源文件,这样不占内存)
原文地址:关于QT下资源使用和资源占用内存过多的问题作者:技术成就梦想 最近研究了一下如何从外部动态调用图片的问题,从而研究了图片资源的使用方法.网上最常见的帖子是这个,感觉总结的还不错. h ...
- 2015必须推荐的Android框架,猿必读系列!
一.Guava Google 的基于java1.6的类库集合的扩展项目,包括collections, caching, primitives support, concurrency librarie ...
- zabbix报警把特定的应用集发送给developer
1.创建新的action 2.创建新的develop用户 3.需要删除组不然会给组里所有人发
- logstash 安装zabbix插件
<pre name="code" class="html">[root@xxyy yum.repos.d]# yum install ruby Lo ...