The National Computer Grade examination two level course-C language Programming _ 14th-structure, common and user-defined types

Source: Internet
Author: User

Example 14.2

Contrast the difference between passing a struct array name to a function and passing a struct variable name to a function.

1#include <stdio.h>2 3typedefstruct4 {5     intnum;6     DoubleMark;7 }rec;8 9 voidsub1 (REC x)Ten { OneX.num = at; AX.mark =81.5; - } -  the voidsub2 (REC y[]) - { -y[0].num = A; -y[0].mark =77.5; + } -  + Main () A { atREC A = { -,90.0}, b[] = { -,90.0 }; - Sub1 (a); -printf"A)%d,%5.1lf\n", A.num, A.mark); - Sub2 (b); -printf"B)%d,%5.1lf\n", b[0].num, b[0].mark); -}

Output Result:

A) 16, 90.0
B) 12, 77.5
Please press any key to continue ...

Example 14.3

Returns the value of a struct type through a function.

1#include <stdio.h>2 3typedefstruct4 {5     intA;6     Charb;7 }st;8 9St Fun (St X)/*The return value type of the function is the struct type St*/Ten { OneX.A = About; Ax.b ='S'; -     returnx; - } the  - Main () - { - ST y; +Y.A =0; -y.b ='A'; +printf"y.a=%d,y.b=%c\n", Y.A, y.b); Ay =Fun (y); atprintf"y.a=%d,y.b=%c\n", Y.A, y.b); -}

Output Result:

Y.a=0,y.b=a
Y.a=99,y.b=s
Please press any key to continue ...

Example 14.4

Returns a pointer to a struct variable through the return value of the function.

1#include <stdio.h>2 3typedefstruct4 {5     intA;6     Charb;7 }st;8 9St * Fun (St X)/*The return value type of the function is St * Type*/Ten { OneST *px; AX.A = -; -x.b ='C'; -PX = &x; the     returnpx; - } -  - Main () + { -ST y, *p; +Y.A =999; Ay.b ='X'; atprintf"y.a=%d y.b=%c\n", Y.A, y.b); -p =Fun (y); -printf"(*p). a=%d (* p). b=%c\n", (*p). A, (*p). b); -}

Output Result:

y.a=999 y.b=x
(*p). a=100 (* p). b=c
Please press any key to continue ...

123

The National Computer Grade examination two level course-C language Programming _ 14th-structure, common and user-defined types

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.