Pointers to C Examples Using Pointers
- Program 3 shows how
scanf uses addresses/pointers to achieve
pass-by-reference, and includes two functions (one broken, one
working) that attempt to get two numbers from a user. The
broken one is broken because the parameters are being passed by
value. The working function uses pointers instead to achieve
the effect of pass-by-reference.
- Program 4 reads 2 numbers to
fill the members of a struct. It passes the struct "by
reference" (really by address/pointer) for space efficiency (the
pointer takes less space than the struct). The
fill2Nums function also illustrates taking the
address of a struct member.
Alyce Brady, Kalamazoo College