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 realize enumeration and pointer in C language

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how C language implements enumerations and pointers". In daily operation, I believe many people have doubts about how C language implements enumerations and pointers. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about how C language implements enumerations and pointers. Next, please follow the editor to study!

Detailed explanation of enumerations and pointers in C language

To sum up, define enumerations with typedef enum keywords, such as typedef enum {Red,Green,Blue} Color3

Enumeration to numeric conversion, if no representative value is specified, it is calculated from 0, such as Color3 centering Red; printf ("% d", c); 0 is displayed, unless specified such as typedef enum {Red=3,Green=5,Blue=10} Color3

For the definition of a type pointer, adding * to the left of the variable name means that the variable is only a null pointer. If you need to assign an initial value, it can only be = 0 or = NULL (NULL is actually 0). The following statements must be given a new address or apply for memory before they can be used.

The use of pointers is also very simple, such as defining int iTunes 10; * pi=0; defines the pointer pi. If you use pi, you will refer to the pointer pi, and if you use * pi, you will indicate the specific value of the reference pi.

For example:

Int iTunes 10, * pi=0; pi= & I; / & is to take the address * pi+=5; printf ("% d", * pi); / / 15 printf ("% d", I); / / because pi points to the address of I, * pi has changed, and I has changed, too.

OK, if the pointer wants to apply for new memory, you can use malloc, such as:

# include... Int* pendant 0; p = (int*) malloc (sizeof (int)); / / request an actual piece of memory for the p pointer * pprinter 12345; / / assign printf ("% d", * p) to the memory referred to by p; / / 12345 free (p); / / release memory

OK, then how to transfer pointer parameters, as follows:

Void testP (int * p) {* paired pointer 100;} void main () {int iTunes 10 * * TestP (& I); testP (& I); printf ("% d", I): / / how much do you think it is?} this is the end of the study on "how C language implements enumerations and pointers", hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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