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