Structures, as well as variables, can be passed as parameters in functions:
When a structure is called by value in a function, its content is copied locally into the function, as for variables.
// Call by value
void fct (struct vector v);
If a function has to modify the content of a structure, a call by reference must be used.
As for variables, passing a structure pointer as a parameter to a function allows it to access its contents in read and write mode.
// Call by reference
void fct (struct vector * v);
Note that in the case of a structure pointer, the fields are accessible with the ->
operator:
// x field of the structure pointer v
v->x
Create a vector
structure that contains the coordinates of a vector in 3 dimensions.
Write a function norm()
that receives a vector as a parameter and returns the norm (length) of the vector.
Here is an example of how to calculate the norm:
x=2.3 y=3.4 z=4.5 The norm of the vector is : 6.090977
Create a RGB
structure that contains three colors (red
, green
and blue
) and a grey
value.
Write a grayscale()
function that computes and updates the grey
field of the structure by averaging the three colors.
$$ grey = \dfrac{red+green+blue}{3} $$
Here is an example:
R = 120 G = 180 B = 64 --------------- Grey = 121
Why is there no real question in this quiz?