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语言的画图(圆形动画)
#include <stdio.h> #include <malloc.h>#include<graphics.h> #define LEN sizeof(stru ...
- bzoj 2075: [POI2004]KAG
整天鬼畜题搞搞,感觉药丸…… 这种题出到xjoi模拟题里,太神了…… 这题的核心在于分割Cograph,尝试把Cograph的合成过程给求出来. 我们将这张图中的边为黑边,在这张图的补图中出现的边为白 ...
- iOS 7 二维码
维码扫描 2014-06-13 10:20:29| 分类: iOS|举报|字号 订阅 下载LOFTER客户端 // // TCTosweepScan.m // TongCheng ...
- 在NGINX上配置HTTPS---血的教训--要重启NGINX
重启,不是重载!!! 是STOP & START 而不是RELOAD!!! 纠结了好几天...(难道有的NGINX上不用重启????) 你妹的,上次也是,,PHP-FPM,将一个PHP的程序连 ...
- 功率和dB的关系
功率和dB的关系应该如下: 1.dB的引入是为了把乘除关系变换为加减,便于工程中的运算. 2.[dB] = 10lg(输出功率W/输入功率W).如:输入功率为1W而输出功率为1000W,则系统的增益为 ...
- java cannot find the tag library descriptor for "http://java.sun.com/jsp/jstl/core"
出现java cannot find the tag library descriptor for "http://java.sun.com/jsp/jstl/core" 主要的愿 ...
- Ubuntu 无线连接能上网,但是有线连接不能上
这两天装Ubuntu,遇到小问题.最头疼的还是上网,过去我装了Ubuntu时,都是插上网线就能直接上网,这次就不行了. 我刚点开一个网页,接下来点就不能上了,但是无线连接就可以正常上网. 我在一个论坛 ...
- zoj 3706 Break Standard Weight(dp)
Break Standard Weight Time Limit: 2 Seconds Memory Limit: 65536 ...
- THINKPHP之控制器
如何新增控制器? 在Lib/Action中新增一个控制器:ProductAction.class.php: <?php class ProductAction extends Action{ ...
- 算法导论——lec 11 动态规划及应用
和分治法一样,动态规划也是通过组合子问题的解而解决整个问题的.分治法是指将问题划分为一个一个独立的子问题,递归地求解各个子问题然后合并子问题的解而得到原问题的解.与此不同,动态规划适用于子问题不是相互 ...