-2

EDIT: Since there is no difference between int * x, int* x, and int *x, I removed that section to better clarify what I'm confused about.

The question:

The differences between "pointers" and "references" are explained in the answer to What are the differences between a pointer variable and a reference variable in C++?, but the syntax itself seems to have different meanings depending on the context.

When a pointer or reference is declared, what do the * and & modifiers make those variables mean later? Are they the pointer, the raw value, the object reference, etc.

For example, since words seem to be failing me here:

void foo(int* x, int& y) {
    assert(*x == ??);
    assert(&x == ??);
    assert(*y == ??);
    assert(&y == ??);
}
ABadHaiku
  • 65
  • 1
  • 6
  • 1
    maybe you got confused by a quirk in the grammar that makes `int * x,y;` declare an `int*` and an `int`, or I dont know what caused your confusion. Anyhow, where you put the blank does not matter – 463035818_is_not_an_ai Mar 31 '21 at 18:29
  • 2
    *but I can't find a hard explanation of what happens when a variable is declared with these modifiers in a method* The exact same thing. A declaration is a declaration, it doesn't matter where it is, it still follows the single set of declaration rules C++ has. – NathanOliver Mar 31 '21 at 18:29
  • 2
    `int* x` is the same as `int *x` is the same as `int * x`. Same for the references `&` . – Richard Critten Mar 31 '21 at 18:30
  • 1
    You don't get a pointer with `*x` and a reference with `&x`, you get an error with `*x` for trying to dereference an `int` and you get a pointer with `&x` because `&x` takes the address of `x` and the value of a pointer is an address. – chris Mar 31 '21 at 18:33
  • 1
    "*if you have a variable declared with int x, you can get the pointer with *x and the reference with &x*" No. If you have a pointer `int* x` then you can have the value this pointer points to by `value= *x;` A pointer is basically an address. If you have a value `x` then you can get its address by `address= &x;` – Ripi2 Mar 31 '21 at 18:35
  • 1
    To be clear, `*` and `&` have different meanings in declarations vs. expressions. Declaring something as a pointer is not the same as dereferencing a variable that's already been declared. I'm not sure if that's what you're stuck on. – chris Mar 31 '21 at 18:38

1 Answers1

2

The rules for this are pretty simple. It does not matter where you see a declaration, if it is in main, some_namespace, inside a class, or in the parameter section of a function, a declaration means the same thing. Knowing that, whenever you see

type_name * variable_name

you are declaring a pointer. Wherever you see

type_name & variable_name

You are declaring a reference. The spacing here doesn't matter, int* foo, int * foo, and int *foo all declare an int* named foo. The only tricky bit here is that the * or & applies to the variable name, not the type. That means in code like

int * foo, bar;

foo and bar are not both pointers. foo is a pointer to an int and bar is just an int. To make bar and int you'd need to use

int * foo, * bar;

And that take care of declarations.

For use, if you see

*variable_name

Then you are indirecting through variable_name (calling the unary operator*). If you see

&variable_name

then you are getting the address of variable_name, or calling operator& of the variable_name's type.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402