1. We will often encounter coercion type conversions in C language.
Here, I introduce a struct pointer type conversion, but there is a premise (somewhat analogous to the cast of a child parent object in C + +).
A Brief introduction:
First we need to know a pointer to a struct, and in this struct, the first struct member must also be a struct (preferably a struct type).
Then we can convert the struct pointer to a pointer to the first member struct in the struct.
Look directly at the code:
/* 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 #include <stdio.h>//define VX_REF-typedef struct _vx_ref{,}vx_ref_t; The typedef struct _VX_REF *vx_ref; //define vx_scale typedef struct _vx_scale{int ab;}vx_scale_t; The _vx_scale *vx_scale of the typedef struct; //define vx_image include the "the", "Vx_ref vx_scale typedef" struct _vx_image {vx_ref_t ab; 27 vx_scale_t AC; int b; }vx_image_t; The typedef struct _VX_IMAGE *vx_image; //the main (+ void Main) 33 {Vx_image A; Define a point to vx_image pointer + A->AB.A = 1;//put to assignment of Sub_struct A->ac.ab = 2; Notoginseng printf ("********the old value*********\n"); %d:\n printf ("The main struct value is%d", a->ab.a,a->ac.ab); Transform printf ("The first structure\n"); Vx_ref p = (vx_ref) a;//make the main struct pointer point to Sub_strcut//printf ("The Transform Secon D structure\n "); * printf ("The Is%d\n", p->a);//output//printf ("We try\n"); Vx_scale q = (Vx_scale) A; //printf ("is%d", a->ab);//output//The conclusion//in the main struct include some sub _strcut//we can use first sub_struct pointer to cast (cast) The main structure pointer Wuyi//Example:sub_strcut Poin ter = (sub_struct) (the main structure pointer)//so W E get sub_struct pointer. //oNly by First Point
Operation Result:
The old value*********
The main struct value is 1 2:
The transform first structure
The is 1
It's just the same as what you think.
Cast of struct pointer type in C language