Passing Pointers to Functions in C
When you pass a pointer to a function, you are sending the address of a variable. This allows the function to directly read or modify the original variable — unlike normal pass-by-value where only a copy is sent.Syntax
Example: Modify a Variable via Pointer
Example: Swap Two Numbers
Without pointers, swapping inside a function has no effect on the originals. With pointers:Pass by Value vs Pass by Reference
Key Points
- Pass
&variableat the call site to send the address - Use
*ptrinside the function to access or modify the value - This is how C achieves pass-by-reference behaviour
- Especially useful for functions that need to return multiple values