Reputation: 61
I have a function as below
void foo(const foo_type* x)
{...}
I am calling this function in other function.
void foo1(int z)
{
foo_type y = z;
foo(&y);
}
This is not throwing any error in c but i feel something is not correct. What happens when i pass non const variable to function that expects const variable as arguments.
Upvotes: 4
Views: 2062
Reputation: 234875
The introduction of const
in the way you have is absolutely fine and even desirable.
You have told the compiler explicitly that the function will not modify the parameter passed as const
. That could even help with a compiler's optimisation strategy.
The removal of const
is, in general, a bad idea though. (Which you're not doing in this case.) The behaviour on modifying a variable via the removal of const
that was originally declared as const
is undefined.
Upvotes: 7