What’s the difference between * and & in C?

* and & as type modifiers

  • int i declares an int.
  • int* p declares a pointer to an int.
  • int& r = i declares a reference to an int, and initializes it to refer to i.
    C++ only. Note that references must be assigned at initialization, therefore int& r; is not possible.

Similarly:

  • void foo(int i) declares a function taking an int (by value, i.e. as a copy).
  • void foo(int* p) declares a function taking a pointer to an int.
  • void foo(int& r) declares a function taking an int by reference. (C++ only)

* and & as operators

  • foo(i) calls foo(int). The parameter is passed as a copy.
  • foo(*p) dereferences the int pointer p and calls foo(int) with the int pointed to by p.
  • foo(&i) takes the address of the int i and calls foo(int*) with that address.

(tl;dr) So in conclusion, depending on the context:

Leave a Comment