The following two blocks of code do the same thing:
void foo(int *num) {...}
int main(void)
{
int num = 5;
foo(&num);
...
}
void foo(int *num) {...}
int main(void)
{
int *num;
*num = 5;
foo(num);
...
}
In both cases foo expects a pointer-to-int argument. In the first I declare an int and use the address operator (&) to pass it into foo. In the second I declare a pointer variable and directly pass it into foo.
Is there a reason to use either one, other than a stylistic preference? I prefer to declare all of my variables as pointers, as in the second, but I see the first a lot more frequently.
-Wall -Wextra -Werror
to your gcc command, as that will show up a lot of dubious code (and the last one makes them appear as errors rather than ignore-able warnings!) – Gwyn Evans