[Programming] C language struct pointer as function parameter, pointer Function

Source: Internet
Author: User

[Programming] C language struct pointer as function parameter, pointer Function

Struct pointer as function parameter:
The struct variable name represents the entire set itself, the entire set passed as a function parameter, that is, all members, rather than being converted into a pointer by the compiler like an array. If there are many struct members, especially when the Members are arrays, the transmission time and space overhead will be great, which affects the program running efficiency. Therefore, the best way is to use the struct pointer. In this case, only an address is transmitted from the real parameter to the form parameter, which is very fast.

 

# Include <stdio. h> struct stu {char * name; int score;} stus [] = {"zhangsan1", 65 },{ "zhangsan2", 98 }}; void averge (struct stu *, int); int main () {int len = sizeof (stus)/sizeof (struct stu); printf ("start... \ n "); // array name can be considered as a pointer averge (stus, len);} void averge (struct stu * stus, int len) {char * name; int score; int sum = 0; for (int I = 0; I <len; I ++) {name = stus [I]. name; // The first form score = (* (stus + I )). score; // second FORM sum + = score; printf ("% s... % d \ n ", name, score);} printf (" average score: % d... \ n ", sum/len );}

 

Related Article

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.