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

Why to use pointer to structure in C language

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Most people do not understand the knowledge points of this article "Why do you use pointing structure pointers in c language", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article "Why point structure pointers are used in c language".

1. The pointer to the structure is usually easier to control than the structure itself.

2. The early structure cannot be passed to the function as an argument, but a pointer to the structure can be passed.

3. Even if you can pass the structure, passing the pointer is usually more efficient.

4. Some structures used to represent data contain pointers to other structures.

Example

# include # define LEN 20 struct names / / define the structure names {char first [LEN]; char last [LEN];}; struct guy / / define the structure guy {struct names handle; char favfood [LEN]; char job [LEN]; float income;} Int main (void) {struct guy fellow [2] = {/ / this is a structural nesting in which the names structure / / initialization structure array fellow is embedded in the guy structure Each element is a structural variable {{"Ewen", "Villard"}, "girlled salmon", "personality coach", 68112.00}, {{"Rodney", "Swillbelly"}, "tripe", "tabloid editor", 432400.00}} Struct guy * him; / / this is a pointer to the structure printf ("address # 1 address% p # 2 printf% p\ n", & fellow [0], & fellow [1]); him = & fellow [0]; / / tells the compiler where the pointer points to printf ("pointer # 1 printf% p # 2printf% p\ n", him,him+1) / / two addresses printf ("him- > income is $% .2f: (* him). Income is $% .2f\ n", him- > income, (* him) .income); / / 68112.00 / / points to the next structure, him plus 1 equals the address pointed to by him plus 84. The names structure takes 40 bytes, favfood 20 bytes, handle 20 bytes, and float 4 bytes, so the address will be added 84 him++; printf ("him- > favfood is% s: him- > handle.last is% s\ n", him- > favfood,him- > handle.last); / / because of the above him++, it points to favfood1 [1], return 0 } the output is PS D:\ Code\ C\ structure > cd "d:\ Code\ C\ structure\"; if ($?) {gcc structDemo02.c-o structDemo02} If ($?) {.\ structDemo02} address # 1:000000000061FD70 # 2:000000000061FDC4pointer # 1:000000000061FD70 # 2virtual 0000000061FDC4hime-> income is $68112.00: (* him). Income is $68112.00 Him-> favfood is tripe: him- > handle.last is Swillbelly above is the content of this article on why pointing structure pointers are used in c language. I believe you all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about it, please 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

Internet Technology

Wechat

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

12
Report