Since a
is global in this case there is no relevant difference, in the first function you are passing a pointer to a
, any chages made to a
in the scope of the function will be reflected in a
outside the scope of the function, even if it's not global.
The second function is acting direclty in the global variable so, the changes are obviously also reflected in a
.
A relevant difference would be noted if you passed a
by value, i.e.:
xyz_t a;
void func3(xyz_t b)
{
printf("%d", b.x); //example 3
}
//...
func3(a);
//...
Here you would be passing a
by value, essentially a copy of a
, any change made to a
in the scope of the function would not really be to a
but to a copy of it, and naturally it would not reflect in the original a
.
Noting that you are not changing anything, only printing the value stored in a
any of the three render the same results.
In this case, because you are not trying to change the variable it's a good practice to use const
keyword to avoid any accidental change.
void func1(const xyz_t* b)
Passing by value also guards against accidental changes but it might make the program run slower if you are dealing with large data structures.