[Zz] use of typedef struct, typedef union, and Union

Source: Internet
Author: User

(1) the well-known use of typedef improves code Portability:

// On the platform that supports long double, the most precise type is real <br/> typedef long double real; <br/> // if the highest precision of the other platform is float <br/> typedef float real; 

 

(2) Use of typedef struct:

Method 1

Struct MSG {<br/> byte index; <br/> int Len; <br/> char data [10]; <br/> }; <br/> struct msg a; <br/>. index = 0x1a; <br/> //... omitted 

Method 2

Typedef struct message {<br/> byte index; <br/> int Len; <br/> char data [10]; <br/>} MSG; // New Type <br/> MSG; <br/> MSG. index = 0x1a; 

Method 3

Struct MSG {<br/> byte index; <br/> int Len; <br/> char data [10]; <br/>}a; <br/>. index = 0x1a; 

 

(3) Use of union

Union intbyte <br/>{< br/> unsigned char byte [4]; <br/> int real; <br/> }; 

 

 

Reference from:

Http://lionwq.spaces.eepw.com.cn/articles/article/item/17197 #

 

Audio/Video Encoding/Decoding Algorithms often involve data compression, sound decoding, image scaling, and other issues.
Here we use an example to recommend a wonderful union usage (this method is provided by equator and used in our company's image processing algorithms ). In this example, the union structure n64u is used to implement the 8-byte n64 type and the single-byte C0 ~ To achieve data compression and decomposition.

 

# Include <stdio. h> <br/> # define in <br/> # define out <br/> # define inout <br/> typedef unsigned long n64; <br/> typedef unsigned int n32; <br/> typedef unsigned short n16; <br/> typedef unsigned char n8; </P> <p> typedef struct _ s8t {<br/> unsigned char C0, C1, C2, C3, C4, C5, C6, C7; <br/>} s8t; </P> <p>/* use n64 and C0 ~ C7 occupies the same memory zone to implement mutual conversion */<br/> typedef Union {<br/> n64 n64; <br/> struct {<br/> n32 l0, L1; <br/>}u32; <br/> struct {<br/> long l0, L1; <br/>}s32; <br/> struct {<br/> unsigned short S0, S1, S2, S3; <br/>}separate; <br/> struct {<br/> short S0, S1, S2, S3; <br/>}s16; <br/> struct {<br/> unsigned char C0, c1, C2, C3, C4, C5, C6, C7; <br/>}u8; <br/> struct {<br/> char C0, C1, C2, C3, c4, C5, C6, C7; <br/>} S8; <br/>} n64u; </P> <p> # define max_data_compressed_num 8 </P> <p> int compress64_8 (in const n64 * SRC, in n16 N, out n8 * DST ); <br/> int uncompress8_64 (in const n8 * SRC, in n16 N, out n64 * DST); </P> <p> int compress64_8 (in const n64 * SRC, in n16 N, out n8 * DST) <br/>{< br/> n64u n64u_data; <br/> Register n64 * n64ptr = (n64 *) SRC; <br/> Register n8 * n8ptr = DST; <br/> n16 I = 0, num = N; </P> <p> If (null = n64ptr | null = n8ptr | n <1) <br/>{< br/> printf ("invalid Param, SRC 0x % x, DST 0x % x, N % d/N ", n64ptr, n8ptr, n); <br/> return 0; <br/>}</P> <p> for (I = 0; I <num; I ++) <br/>{< br/> n64u_data.n64 = * n64ptr ++; </P> <p>/* use the simple mean method to compress eight points into one point */<br/> * n8ptr ++ = (n64u_data.u8.c0 + n64u_data.u8.c1 + n64u_data.u8.c2 +/<br/> n64u_data.u8.c3 + n64u_data.u8.c4 + n64u_data.u8.c5 +/<br/> n64u_data.u8.c6 + n64u_data.u8.c7) /(sizeof (n64)/sizeof (n8); <br/>}< br/> return 1; <br/>}</P> <p> int uncompress8_64 (in const n8 * SRC, in n16 N, out n64 * DST) <br/>{< br/> n64u n64u_data; <br/> Register n64 * n64ptr = DST; <br/> Register n8 * n8ptr = (n8 *) SRC; <br/> Register n8 n8data; <br/> n16 I = 0, num = N; </P> <p> If (null = n64ptr | null = n8ptr | n <1) <br/>{< br/> printf ("invalid Param, SRC 0x % x, DST 0x % x, N % d/N ", n64ptr, n8ptr, n); <br/> return 0; <br/>}</P> <p> for (I = 0; I <num; I ++) <br/> {<br/> n8data = * n8ptr ++; </P> <p>/* decompress one point into eight points and adopt simple restoration, so there is distortion */<br/> n64u_data.u8.c0 = n8data; <br/> latency = n8data; <br/> n64u_data.u8.c2 = n8data; <br/> n64u_data.u8.c3 = n8data; <br/> latency = n8data; <br/> n64u_data.u8.c5 = n8data; <br/> n64u_data.u8.c6 = n8data; <br/> n64u_data.u8.c7 = n8data; </P> <p> * n64ptr ++ = n64u_data.n64; <br/>}< br/> int main (INT argc, char * argv []) <br/>{< br/> n64 n64data [max_data_compressed_num]; <br/> n8 n8data [max_data_compressed_num]; <br/> s8t s8t_data [max_data_compressed_num] <br/>=< br/> {/* data to be compressed, one data occupies one byte */<br/> {1, 2, 3, 4, 5, 6, 7, 8},/* 8 bytes are compressed into a single byte */<br/> {2, 2, ,}, <br/> {,}, <br/> }, <br/> {,}, <br/> {,}, <br/>, }, <br/> {,} <br/>}; </P> <p> n16 I, n = max_data_compressed_num; </P> <p> printf ("data:/N"); <br/> for (I = 0; I <n; I ++) <br/> {<br/> n64data [I] = * (n64 *) & s8t_data [I]; </P> <p> printf ("% 3u % 3u % 3u % 3u % 3u % 3u % 3u % 3u/N", <br/> s8t_data [I]. c0, s8t_data [I]. c1, s8t_data [I]. c2, <br/> s8t_data [I]. c3, s8t_data [I]. c4, s8t_data [I]. c5, <br/> s8t_data [I]. c6, s8t_data [I]. c7); <br/>}< br/> printf ("/N"); </P> <p> compress64_8 (n64data, N, n8data ); </P> <p> printf ("compressed to:/N"); <br/> for (I = 0; I <n; I ++) <br/>{< br/> printf ("% 3u", n8data [I]); <br/>}< br/> printf ("/n"); </P> <p> uncompress8_64 (n8data, N, n64data ); </P> <p> printf ("uncompressed to:/N"); <br/> for (I = 0; I <n; I ++) <br/>{< br/> * (n64 *) & s8t_data [I] = n64data [I]; </P> <p> printf ("% 3u % 3u % 3u % 3u % 3u % 3u % 3u % 3u/N", <br/> s8t_data [I]. c0, s8t_data [I]. c1, s8t_data [I]. c2, <br/> s8t_data [I]. c3, s8t_data [I]. c4, s8t_data [I]. c5, <br/> s8t_data [I]. c6, s8t_data [I]. c7); <br/>}< br/> printf ("/N "); <br/>}</P> <p>/* <br/> running result: <br/> data: <br/> 1 2 3 4 5 6 7 8 <br/> 2 2 2 3 4 5 6 7 7 <br/> 3 2 3 4 5 6 7 6 <br/> 4 2 3 4 5 6 7 5 <br/> 5 2 3 4 5 6 7 4 <br/> 6 2 3 4 5 6 7 3 <br/> 7 2 3 4 5 6 7 2 <br/> 8 7 6 5 4 3 2 1 <br/> compressed: <br/> 4 4 4 4 4 4 4 4 <br/> uncompressed: <br/> 4 4 4 4 4 4 4 4 4 <br/> 4 4 4 4 4 4 4 4 <br/> 4 4 4 4 4 4 4 4 <br/> 4 4 4 4 4 4 4 4 <br/> 4 4 4 4 4 4 4 <br/> 4 4 4 4 4 4 4 4 <br/> 4 4 4 4 4 4 4 4 <br/> 4 4 4 4 4 4 4 <br/> */<br/> 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.