2

I am having troubles with correct understanding of those types:

for:

int* j;
int** k;
  1. *j

  2. &j

  3. **j

  4. *&j

  5. *k

  6. &k

  7. **k

  8. *&k

  9. &*k

My thoughts:

  1. int** - double int pointer?

  2. address for j pointer - whats the type of address? (Hexadecimal value)

  3. int*** ?

  4. pointer, pointing to an address of j pointer ?

  5. int***

  6. address to double pointer k

  7. int****

  8. ?

  9. ?

Sahib Yar
  • 1,030
  • 11
  • 29
Piodo
  • 616
  • 4
  • 20
  • 2
    `*` in declarations in used to declare pointer, but in statements it can be used to dereference a pointer to invoke multiplication operator. So 1) `*j` given that type of `j` is `int *` will produce `int &`. `&` in declarations is used to declare lvalue reference, but in statements it can be used to take address or to invoke *bitwise and* operator. – user7860670 Aug 13 '17 at 22:28
  • 1
    I suggest reading [this](https://stackoverflow.com/questions/57483/what-are-the-differences-between-a-pointer-variable-and-a-reference-variable-in?rq=1) and see where your understanding lies. – D P Aug 13 '17 at 22:36
  • 1
    Hint: `int * j` means not only "`j` is an `int *`" but also "`* j` is an `int`". This is intentional. – MSalters Aug 13 '17 at 22:42

1 Answers1

5

As mentioned in comments * and & have different semantics depending if they appear in declarations or statements:

  1. *j Dereferences j
  2. &j Takes the address of j
  3. **j Double dereferencing a single pointer (Error)
  4. *&j Dereferences the address of j (equivalent to j)
  5. *k Dereferences k (yields another pointer)
  6. &k Takes the address of k
  7. **k Double dereferences a double pointer (OK)
  8. *&k Dereferences the address of k (equivalent to k)
  9. &*k Takes the address of the dereferenced pointer

See a live demo


Note:

address for j pointer - whats the type of address? (Hexadecimal value)

The hexadecimal value is only used as usual representation for pointers, otherwise these are just numbers / values. Hexadecimal doesn't qualify for a type, it's just a numerical representation.

user0042
  • 7,917
  • 3
  • 24
  • 39
  • Thanks for answer. So *& negates each other and the statement *&k =k is true? Also another question - We can only dereference x times the variable only if it has been referenced at least x times right? (Like, we can't double dereference, single pointer, but we are able to triple dereference 5th level pointer?) – Piodo Aug 14 '17 at 09:32
  • @Piodo Regarding your additional question: Yes. Dereferencing a `int***` once yields a `int**` and twice yields a `int*`. – user0042 Aug 14 '17 at 09:35