Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm learning C and I'm still not sure if I understood the difference between & and * yet.

Allow me to try to explain it:

int a; // Declares a variable
int *b; // Declares a pointer
int &c; // Not possible

a = 10;
b = &a; // b gets the address of a
*b = 20; // a now has the value 20

I got these, but then it becomes confusing.

void funct(int a) // A declaration of a function, a is declared
void funct(int *a) // a is declared as a pointer
void funct(int &a) // a now receives only pointers (address)

funct(a) // Creates a copy of a
funct(*a) // Uses a pointer, can create a pointer of a pointer in some cases
funct(&a) // Sends an address of a pointer

So, both funct(*a) and funct(&a) are correct, right? What's the difference?

like image 326
Cristian Ceron Avatar asked Feb 28 '15 05:02

Cristian Ceron


People also ask

What is the difference between * and & in C++?

The & is a unary operator in C which returns the memory address of the passed operand. This is also known as address of operator. <> The * is a unary operator which returns the value of object pointed by a pointer variable. It is known as value of operator.

What does-> mean in c++?

The -> is called the arrow operator. It is formed by using the minus sign followed by a greater than sign. Simply saying: To access members of a structure, use the dot operator. To access members of a structure through a pointer, use the arrow operator. cpp_operators.htm.


2 Answers

* 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:

  • * can be either the dereference operator or part of the pointer declaration syntax.

  • & can be either the address-of operator or (in C++) part of the reference declaration syntax.

    Note that * may also be the multiplication operator, and & may also be the bitwise AND operator.

like image 141
emlai Avatar answered Oct 15 '22 09:10

emlai


funct(int a) 

Creates a copy of a

funct(int* a) 

Takes a pointer to an int as input. But makes a copy of the pointer.

funct(int& a)

Takes an int, but by reference. a is now the exact same int that was given. Not a copy. Not a pointer.

like image 28
Michael LeVan Avatar answered Oct 15 '22 09:10

Michael LeVan