Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ampersand "&" do in front of pointers?

When functions are being called I often see the ampersand in front of the pointer in the function parameter.

E.g.

int *ptr;
randomFunction(&ptr);

I have done some research and found that this means that the function uses pointers to pointers. Is the & sign in front of a pointer used just to indicate this or does it do something else?

like image 670
user3213163 Avatar asked Jan 22 '14 03:01

user3213163


People also ask

What does an ampersand represents?

An ampersand is a sign for the word and. It's written or typed as the symbol &. It's a modification of the term “and per se and,” which has Latin origins. The ampersand can indicate that the listed items are grouped together as part of a name.

What does the & symbol represent?

An ampersand is a symbol (&) representing the word and. The ampersand was included in the Old English alphabet, and the term is an alteration of and per se and.

What is the Asperand symbol?

Noun. The symbol @; at sign.

Why do people decorate with ampersands?

The most common symbolism of an ampersand in interior home design is as a symbol of commitment: “You & Me.” You've probably seen ampersands in wedding decor as well. It shows that two people are joined — they are each others' “and,” meaning there is not one without the other.


3 Answers

It's a pointer to the pointer.

& is the reference operator, and can be read as address of. In your example, it will get another pointer, that is the address of the pointer given as it's argument, i.e. a pointer to the pointer.

Look at the following example:

int **ipp;
int i = 5, j = 6, k = 7;
int *ip1 = &i, *ip2 = &j;
ipp = &ip1;

You will get:

enter image description here

In the above example, ipp is a pointer to pointer. ipp stores the address of ip1 and ip1 stores the address of i.

You can check out Pointers to Pointers for more info.

like image 36
herohuyongtao Avatar answered Oct 16 '22 14:10

herohuyongtao


Take a step back. The fundamental rules of pointer operators are:

  • The * operator turns a value of type pointer to T into a variable of type T.
  • The & operator turns a variable of type T into a value of type pointer to T.

So when you have

int *ptr;

ptr is a variable of type pointer to int. Therefore *ptr is a variable of type int -- the * turns a pointer into a variable. You can say *ptr = 123;.

Since ptr is a variable of type pointer to int, &ptr is a value -- not a variable -- of type pointer to pointer to int:

int **pp = &ptr;

&ptr is a value of type pointer to pointer to int. pp is a variable of type pointer to pointer to int. *pp is a variable of type pointer to int, and in fact is the same variable as ptr. The * is the inverse of the &.

Make sense?

like image 138
Eric Lippert Avatar answered Oct 16 '22 15:10

Eric Lippert


It helps to think of "&" this way. int function_name ( &( whatever ) ); You are passing the address of ( whatever ). Whatever can be a number of things: an elementary variable. a function. a structure. a union. an array. You should mentally translate "&" to "take the address of". So your example means : pass a COPY of the address of the address of the variable ptr of type int!

like image 1
user3221497 Avatar answered Oct 16 '22 14:10

user3221497