Passing Structures to Functions
If a function requires a structure as a parameter, you will pass it to the function like any ordinary variable. In the code example below, there are two variables: a and b, declared with the type complex. The function display() takes one parameter of type complex. So, you can simply pass either the variable a or b to the function display() as shown below.
Example
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
typedef struct{ float re; float im;} complex;void display(complex x){ printf(“(%f + j%f)\n”, x.re, x.im);}int main(void){ complex a = {1.2, 2.5}; complex b = {3.7, 4.0}; display(a); display(b);} |