Skip to main content

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

Output:

Example: Swap Two Numbers

Without pointers, swapping inside a function has no effect on the originals. With pointers:
Output:

Pass by Value vs Pass by Reference


Key Points

  • Pass &variable at the call site to send the address
  • Use *ptr inside 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