0

I've seen many different ways of writing the address-of operator (&) and the indirection operator (*)

If I'm not mistaken it should be like this:

//examples
int var = 5;
int *pVar = var;

cout << var << endl; //this prints the value of var which is 5

cout << &var << endl; //this should print the memory address of var

cout << *&var << endl; //this should print the value at the memory address of var

cout << *pVar << endl; //this should print the value in whatever the pointer is pointing to

cout << &*var << endl; //I've heard that this would cancel the two out

What would happen if for example you wrote &var as & var with a space between the two? Common syntax I have seen: char* line = var;, char * line = var;, and char *line = var;.

DConine
  • 9
  • 1
  • 2
    Have you tried this out? – aschepler Apr 08 '17 at 00:49
  • Related (in C: http://stackoverflow.com/a/30423656/335858) – Sergey Kalinichenko Apr 08 '17 at 00:51
  • In this area, whitespace is significant *only* to the extent that it's used to separate tokens. For example, if you have `char const *foo`, you need to leave the space between `char` and `const` (to keep it from being treated as a single token `charconst`), but the rest is irrelevant--you can remove or insert as much white space on each side of the `*` as you want, and it makes no difference to the meaning (likewise with `&`). There are widely diverging opinions as to what use of white space maximizes readability. – Jerry Coffin Apr 08 '17 at 00:55
  • 1
    https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rl-ptr – Galik Apr 08 '17 at 01:00
  • A modern compiler should stop right here: `int *pVar = var;`. An integer is not a pointer. (This was legal in C, but my C++ doesn't go back far enough to know for certain if it was ever legal in C++) All compilers should reject `&*var` because before applying `&` it will apply `*` and you can only `*`, dereference, a pointer. – user4581301 Apr 08 '17 at 01:02

1 Answers1

1

Firstly, int *pVar = var; is incorrect; this does not store the address of var, but it stores the address "5", which will lead to a compilation error saying:

main.cpp: In function 'int main()':
main.cpp:9:15: error: invalid conversion from 'int' to 'int*' [-fpermissive]
    int *pVar = var;
                ^~~

var needs to be references in the initialization of *pvar:

int *pVar = &var;

Secondly, cout << &*var << endl; will also cause an error at compilation because var is not a pointer (int*) type variable:

main.cpp: In function 'int main()':
  main.cpp:19:13: error: invalid type argument of unary '*' (have 'int')
     cout << &*var << endl; //I've heard that this would cancel the two out
               ^~~

Now, to answer your question, adding whitespace between the referencing (&) operator and the pointer (*) operator will make absolutely no difference to the compiler. The only time it makes a difference is when you want to separate 2 tokens; like const and string for example. Running the following code just to exaggerate an example of what you are asking:

cout <<             &             var << endl;  
cout <<                            *                    & var << endl;
cout <<                                   *pVar << endl;

Yields the same results as those from code without so many spaces:

0x7ffe243c3404
5
5
BusyProgrammer
  • 2,783
  • 5
  • 18
  • 31