0

One of the things people new to C/C++ don't realize is that * and & aren't always the dereferencing and address of operators. Not once I had to clarify this in a SO answer, but I found it hard to word it in a correct and clear way.

A)

int *p = ...
int &r = ...

B) the dereference / address of operators.


  1. what is the context difference between the two?

    A. is... inside a type declaration?

    B. is... any other expression...

  2. What are the symbols called in A?

A pedantic, standard wording would certainly be appreciated, but the intention here is to explain this to beginners.

Clarification: this is about pointers / references context, so I just ignore the obvious arithmetic, bitwise operators.

bolov
  • 72,283
  • 15
  • 145
  • 224
  • And a parenthesis is used to change operator preceedence and for a function call., etc. In natural languages the same word can have different meanings. It is not really a problem, just learn. This is explained everywhere in the web or any good C book. – too honest for this site Nov 02 '15 at 21:13
  • Operators operate on an expression. In `int *p`, `p` isn't an expression, it's introducing the name of a variable – M.M Nov 02 '15 at 22:14

3 Answers3

1

When * and & appear in a type definition they are creating a pointer and reference type, respectively. (e.g., int* "int pointer" or "pointer-to-int", int& "int reference" or "reference-to-int").

As part of an expression, the * and & are context sensitive and the expression determines their interpretation.

* operator:

  • indirection (i.e., dereference a pointer), see "Member access operators" below
  • multiplication, see "Arithmetic operators" below

& operator:

See also:

James Adkison
  • 9,412
  • 2
  • 29
  • 43
  • do they have a special name like for instance `type qualifiers`? – bolov Nov 02 '15 at 20:48
  • 1
    its a datatype. i.e. int* and int are two different data types – DTSCode Nov 02 '15 at 20:51
  • I know they are not type qualifiers, I just wondered if as `const` and `volatile` are called `type qualifiers`, maybe `*` and `&` are called something too. – bolov Nov 02 '15 at 20:53
  • @bolov they are. data types. – DTSCode Nov 02 '15 at 20:54
  • 2
    @bolov Don't think of `int*` as 2 pieces. It is one thing, it is the "type." (e.g., it would be as if they used an `intp` keyword for the `int` pointer type but the chosen syntax works for all types). – James Adkison Nov 02 '15 at 20:57
  • [C++11](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf) doesn't have a generic term for this kind of declaration modifier; they're spread over a bunch of different grammar rules. See section 8, "Declarators". – zwol Nov 02 '15 at 20:58
  • @JamesAdkison That is bad advice, which will lead the OP to misunderstand more complicated declarations. – zwol Nov 02 '15 at 20:59
  • @zwol Fair point, I was keeping it specific to the specific example of `int` vs `int*` not more complex uses. – James Adkison Nov 02 '15 at 21:01
  • @zwol Why not capture/add a specific example of how that could go awry in the comments here? – James Adkison Nov 02 '15 at 21:03
  • @JamesAdkison Sorry, I don't have enough brain for that today. – zwol Nov 02 '15 at 21:03
  • @JamesAdkison: `int *ip, i;` is clearly different from `typedef intp *IPT; intp ip, i;`. That's why one should have the space left of the `*`, not at the right (**visual** binding). – too honest for this site Nov 02 '15 at 21:06
  • "When * and & appear as part of an expression they are the dereference and addressof operators, respectively." Is wrong. The note makes this statement even more confusing. – too honest for this site Nov 02 '15 at 21:07
  • @Olaf, I agree that my wording is too simple for a general case. I tried to delete my answer but was not allowed since it was accepted. Therefore, I tried to clean up the wording. – James Adkison Nov 03 '15 at 13:38
1

In c++ '*' and '&' can have one of three meanings (not including meanings for (nonstandard) overloaded operators)

The * operator is typically used for one of the following operations:

  • Multiplication of floating point and integer numbers (e.g. 4*3)
  • Declaring a pointer (e.g. int *foo = 4;)
  • Dereferencing a pointer (e.g. print(*foo);)

The & operator is typically used for one of the following operations:

  • Bitwise and (e.g. 4 & 8)
  • Declaring a reference (e.g.int new_foo = 3; int &bar = new_foo;)
  • Obtaining the address of a variable (e.g. print(&new_foo);)
PC Luddite
  • 5,883
  • 6
  • 23
  • 39
DTSCode
  • 1,062
  • 9
  • 24
0

A) As part of type declaration:

Example: int* p; The * in this context means that p is a pointer variable of type int. The values it can hold are addresses of int variables. Not addresses of char, of float, of double or anything else, only addresses of int. And it can't hold int values, that's the job of plain int variables.

Example: int& p; The & in this context means that p is an alias for another variable. We have chosen to give some other variable a second name, the name p. That's all.

B) Elsewhere:

Example: cout << *p; The * in this context means "dereference p". p is a pointer and as a pointer of a certain type it stores an address of a variable of that type. Here we are telling it, don't give us the address. Give us the value next to that address. In other words, the value held by the variable the address of which the p pointer was pointing to.

Example: cout << &p; The & in this context means the opposite of the previous example. Here, p is a variable of a certain type and we are not telling it to give us its value, but its address.

The moral of the story is that each variable in programming is associated with two numbers: 1) its address or location in memory; 2) the value that it holds.

Want a variable to hold memory locations of specific types? Declare it a pointer with a *. Want a variable to have an alias? Declare the alias with an &. Want a pointer to display, not its address but the value of the variable that it points to? Dereference it with an *. Want to find out the address of a plain old variable? Use the & operator so that you don't get its value.

dspfnder
  • 1,135
  • 1
  • 8
  • 13