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的更多相关文章

  1. C语言中全局结构体指针隐含的错误

    前天在嵌入式系统上,调试一个数组的全局变量时,发现该变量一直会动态变化.深入分析, 才发现该全局结构体没有申请内存,而是用了一个指针.这种情况编译器是检查不出来的,在linux 上运行会挂掉,但是在裸 ...

  2. go语言之进阶篇结构体指针类型匿名字段

    1.结构体指针类型匿名字段 示例: package main import "fmt" type Person struct { name string //名字 sex byte ...

  3. C语言中的结构体,结构体数组

    C语言中的结构体是一个小难点,下面我们详细来讲一下:至于什么是结构体,结构体为什么会产生,我就不说了,原因很简单,但是要注意到是结构体也是连续存储的,但要注意的是结构体里面类型各异,所以必然会产生内存 ...

  4. C语言中的结构体

    用户自己建立自己的结构体类型 1.  定义和使用结构体变量 (1).结构体的定义 C语言允许用户自己建立由不同类型数据组成的组合型的数据结构,它称为结构体. (2).声明一个结构体类型的一般形式为: ...

  5. Verilog缺少一个复合数据类型,如C语言中的结构体

    https://mp.weixin.qq.com/s/_9UsgUQv-MfLe8nS938cfQ Verilog中的数据类型(Data Type)是分散的,缺少一个复合数据类型:把多个wire, r ...

  6. C语言中访问结构体成员时用‘.’和‘->’的区别

    举个例子,定义了一个叫Student,别名为stu的结构类型,我们声明了一个结构体变量叫stu1,声明了一个结构体指针为stuP. typedef struct Student { char name ...

  7. C语言中的结构体和C++中的结构体以及C++中类的区别

    c++中结构体可以定义一个函数 C中的结构体和C++中结构体的不同之处:在C中的结构体只能自定义数据类型,结构体中不允许有函数,而C++中的结构体可以加入成员函数. C++中的结构体和类的异同: 一. ...

  8. C语言中处理结构体的原理

    汇编中有几种寻址方式,分别是直接寻址:(ds:[idata]).寄存器间接寻址(ds:[bx]).寄存器相对寻址(ds:[bx + idata].ds:[bx + si])基址变址寻址(ds:[bx ...

  9. 嵌入式-C语言:通过结构体指针操作结构体内容

    #include<stdio.h> #include<string.h> struct Student { char name[32]; int age; int height ...

随机推荐

  1. Jetty开发(2)

    部署web应用 配置了部署模块的Jetty服务器实例能够在webapps目录下热部署web应用.在webapps目录下标准的War包和jetty的配置文件能够被热部署进服务器需要符合下述规则: exa ...

  2. Caffe : Layer Catalogue(2)

    TanH / Hyperbolic Tangent 类型(type):TanH CPU 实现: ./src/caffe/layers/tanh_layer.cpp CUDA.GPU实现: ./src/ ...

  3. android特效

    http://houxiyang.com/archives/89/ http://blog.csdn.net/hjj0212/article/details/8535817 http://www.li ...

  4. C# 二进制,十进制,十六进制

    //十进制转二进制 Console.WriteLine(Convert.ToString(69, 2)); //十进制转八进制 Console.WriteLine(Convert.ToString(6 ...

  5. ELK 下载地址elastic

    lasticsearch 2.3.5 Elasticsearch can also be installed from our repositories using apt or yum. See R ...

  6. 【转】Android中BindService方式使用的理解

    原文网址:http://www.cnblogs.com/onlylittlegod/archive/2011/05/15/2046652.html 最近学习了一下Android里面的Service的应 ...

  7. Unity性能优化

    一.优化组件访问方式 原文:http://blog.csdn.net/lijing_hi/article/details/11657887 1.缓存Component的引用,如transform 2. ...

  8. js获取url传递参数(转的,原作不详)

    这里是一个获取URL带QUESTRING参数的JAVASCRIPT客户端解决方案,相当于asp的request.querystring,PHP的$_GET 函数: <Script languag ...

  9. AJAX上传文件

    function up_files() { var fileSelect = document.getElementById('file-select'); var files = fileSelec ...

  10. 关于C#中的弱引用

    本文前部分来自:http://www.cnblogs.com/mokey/archive/2011/11/24/2261605.html 分割线后为作者补充部分. 一:什么是弱引用 了解弱引用之前,先 ...