Parameters in C are not necessarily allocated in the stack (*), but their scope will necessarily be restricted to the tomato
function block, and they will necessarily be passed by value.
When you dereference a
and b
in the assignment *foo = *a + *b
, you are interpreting the memory address stored in pointers a
and b
as integers, summing them, and writing the result in the memory address stored in pointer foo
(which, in your example, happens to be in the heap).
After the assignment, you could change a
and b
at will by assigning different memory addresses to them (i.e. pointers), and there would be no consequence to any external memory references as their scope is limited to the function block (e.g. a = foo
). If, however, you changed the memory contents referred by them (e.g. *a = 0
), this would become visible outside the scope of the function as you would be writing on a memory space (stack or heap) allocated somewhere else.
(*) Parameters may not be passed in memory (i.e. stack) to functions. Depending on the compiler/architecture, they may be directly assigned to a processor register. Either way, this is a transparent compiler optimization and you don't have to worry about it... the parameters will behave just the same.