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 ...
随机推荐
- C语言写猜拳游戏中遇到的函数循环小问题
各位可能在初学C语言的时候都有写过猜拳游戏.但在写猜拳的函数时,避免不了会使用循环. 当函数被套在一个循环中的时候,你的计分变量可能就会被重置为函数体里的初始值.那么怎么解决这个问题? 其实很简单,你 ...
- z-index 所遇问题
document.getElementById('wx_share_img').style.cssText = "width:100%;height:100%;position:fixed; ...
- jquery 实现全选反选
jquery代码 $(function () { $('#inputCheck').click(function () { if ($(this).attr("checked")) ...
- <转载>Win32控制台工程中创建窗口
有的时候,用控制台同步输出调试信息.程序状态量,比出Log.弹出报错对话框等方法来得有效.那么如何做到呢?如下: 简而言之,用GetModuleHandle()函数获得当前程序实例句柄,其它地方与常见 ...
- Django架设blog步骤
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- HTML--鼠标事件
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- tomcat动态映射路径
写了一个工具类,将上传文件功能保存文件的目录移到webapps目录外面,通过动态生成xml映射文件到tomcat\conf\Catalina\localhost目录下从而实现目录映射.可以被http直 ...
- HDU2206 IP的计算 【经典题】
IP的计算 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- StretchDIBits使用方法
转自:http://blog.csdn.net/giantchen547792075/article/details/6996011 StretchDIBits 函数把DIB.JPEG.PNG图像中一 ...
- Mongo服务器集群配置【转】
http://www.cnblogs.com/wly923/tag/MongoDB/ 当前标签: MongoDB Mongo服务器集群配置学习三——分片 风行影者 2013-04-14 22:35 ...