Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use struct and union in C language

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article editor for you a detailed introduction of "C language struct and union how to use", the content is detailed, the steps are clear, the details are handled properly, I hope this "C language struct and union how to use" article can help you solve doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.

1. Struct's little secret

Struct in C language can be regarded as a collection of variables.

Struct's question: how much memory does an empty structure take up? Let's write a program and take a look at it:

# include struct TS {}; int main () {struct TS T1; struct TS T2; printf ("sizeof (struct TS) =% d\ n", sizeof (struct TS)); printf ("sizeof (T1) =% d, & T1 =% p\ n", sizeof (T1), & T1); printf ("sizeof (T2) =% d, & T2 =% p\ n", sizeof (T2), & T2); return 0;}

Here is the output. You can see that the size of the empty construct in the gcc compiler is 0.

Structure and flexible array

A flexible array is an array whose size is undetermined.

Flexible arrays can be generated by structures in C language.

The last element of a structure in C can be an array of unknown size.

Think about it, how much memory does the following statement take up? yes, it is 4.

The array in SoftArray is just an identifier to be used and does not take up storage space.

Let's take a look at the code used in a flexible array:

# include#include struct SoftArray {int len; int array [];}; struct SoftArray* create_soft_array (int size) {struct SoftArray* ret = NULL; if (size > 0) {ret = (struct SoftArray*) malloc (sizeof (struct SoftArray) + sizeof (int) * size); ret- > len = size;} return ret;} void delete_soft_array (struct SoftArray* sa) {free (sa) } void func (struct SoftArray* sa) {int I = 0; if (NULL! = sa) {for (I = 0; I)

< sa->

Len; iTunes +) {sa- > array [I] = I + 1;}} int main () {int I = 0; struct SoftArray* sa = create_soft_array (10); func (sa); for (I = 0; I

< sa->

Len; iTunes +) {printf ("% d\ n", sa- > array [I]);} delete_soft_array (sa); return 0;}

The following is the output:

Although the array [] array does not specify a size when it is defined, it can be specified later using the malloc function. The advantage of flexible arrays is that the func () function has a legal pointer to the flexible array without passing the size of the array to be processed.

3. Union in C language

Union in C language is similar to struct in syntax.

Union only allocates space for the largest member, and all members share this space

As follows:

The use of union is affected by the size of the system.

As follows, the storage location of the defined literal quantity constant I at the size end:

The address value stores low-order data in small-end mode and high-order data in large-segment mode.

Let's write a program to determine the size of the system:

# include int system_mode () {union SM {int i; char c;}; union SM sm; sm.i = 1; return sm.c;} int main () {printf ("System Mode:% d\ n", system_mode ()); return 0;}

The following is the output:

So my computer system is a small end.

After reading this, the article "how to use struct and union in C language" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report