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 ...
随机推荐
- ExtJS4.2.1
ExtJS4.2.1 1. 介绍 1.1 说明 ExtJS是一个用javascript.CSS和HTML等技术实现的主要用于创建RIA即富客户端,且与后台技术无关的前端Ajax框架. 常用于企业内部管 ...
- App 冷启动:给 Android 的 Activity 添加一个背景
2016/8/8 11:11:18 # 纠错 之前写的这篇内容的知识点有误,给大家造成了误导,深感抱歉. android 中给 Activity 设置背景的方法是在 style 文件中设置 windo ...
- google jam 比赛题(设计有问题)
题目: Problem Do you know how to read the phone numbers in English? Now let me tell you. For example, ...
- 怎么通过网站优化来增强SEO效果?
真正好的网站优化不应该针对哪一个搜索引擎,而应该关注用户的需求.搜索引擎把这这用户指标权重提升,实际上也是希望把更多的精力集中在用户体验的提升上面.那么,这些用户指标应该怎样提升呢?方法多种多样,下面 ...
- iptables 顺序
-A INPUT -s 115.236.6.6/32 -p udp -m udp --dport 111 -j ACCEPT -A INPUT -s 10.175.197.98/32 -p udp - ...
- java开发经验分享(四)
四. 关于测试 1. 在整个项目计划中,测试时间安排的合理性,对测试阶段的情况应作充分预计,不可为了赶发布点而忽略质量. 2. 务必清楚产品包.更新包.bug包的提交规范.具体请参照<开发规范手 ...
- tyvj1185营业额统计
描述 Description 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司 ...
- 【转】iOS 解决ipv6问题
解决ipv6的方法有很多种,由于现在国内的网络运营商还在使用ipv4的网络环境,所以appstore应用不可能大范围去修改自己的服务器, 而且国内的云服务器几乎没有ipv6地址. 这里附上苹果开发平台 ...
- 一台机器同时运行多个appium实例
测试需要同时在多个android设备上运行,就需要启动多个appium 第一台是运行微信: DesiredCapabilities capabilities = new DesiredCapabili ...
- HBase面试问题
一.HBase的特点是什么 1.HBase一个分布式的基于列式存储的数据库,基于hadoop的hdfs存储,zookeeper进行管理. 2.HBase适合存储半结构化或非结构化数据,对于数据结构字段 ...