What's the Meaning of * and & When Applied to Variable Names

What's the meaning of * and & when applied to variable names?

The unary prefix operator &, when applied to an object, yields the address of the object: &obj.

The type modifier &, when applied to a variable about to be declared, will modify the variable's type to be a reference type: int&.

The same applies to *: When applied as a unary prefix operator to a pointer, it will dereference the pointer, yielding the object referred to: *ptr.

When used as a type modifier to a variable about to be declared, * will modify the type to be a pointer: int*.

In a similar way, the type modifier [] applied to a variable that's being declared will modify the variable's type to an array, while the binary infix operator [] applied to an object of array type will access one of the array's sub-objects.


It's not helpful that type modifiers apply to the variable that is declared, not to the type they are declared with. For example, this

int *p, **pp, i, a[10], &r = i; 

defines an int pointer, a pointer to a pointer to an int, a vanilla int, an array of 10 int, and an int reference. (The latter is immediately initialized, because you cannot have an uninitialized reference.) Note that the type modifiers syntactically belong to the declared variable whose type they are modifying, not to the declared variable's type. Nevertheless, type modifiers (* and &) modify the type of the variable.

In the following case, however, with p, i, and a presumed to be variables that have already been declared

*pp = &i;
a[0] = i;

* and & are unary prefix operators dereferencing pp and yielding the address of i, while [] yields the first int object in the array a.

The fact that C and C++ don't care about the whitespaces around type modifiers and that this led to different camps when it comes to place them doesn't really make things easier.

Some people place the type modifiers close to the type. They argue that it modifies the type and so it should go there:

int* ptr;

The disadvantage is that this becomes confusing when declaring several objects. This

int* a, b;

defines a to be a pointer to int, but b to be an int. Which is why some people prefer to write

int *ptr;
int *a, *b;

I suggest to simply never declare multiple objects in the same statement. IMO that makes code easier to read. Also, it leaves you free to pick either convention.


To complicate things even further, besides the type modifiers and the unary prefix operators & and *, there are also the binary infix operators & and *, meaning "bitwise AND" and "multiplication". And to add insult to injury, in C++ you can overload both the unary prefix and the binary infix variants of these operators (and the binary infix []) for user-defined types and be completely free as to their semantics.

C++ beginner question: dereference vs multiply

C, and by inheritance C++, are swamped with operators and are inherently context-sensitive. You will have to get used to it:

If * appears before the name of a variable that is being declared (or defined), it's a type modifier and makes that variable a pointer.

If it is a unary prefix operator for a variable that is part of an expression, it's dereferencing (or whatever it's been overloaded to).

If it is a binary infix operator for two variables that are part of an expression, it's multiplication (or whatever it's been overloaded to).

(From this you can see that the * in your unsigned char * pixel isn't a dereferencing unary prefix, but a type modifier.)

Note that & pretty much resembles *, only it's meaning is different: it makes a variable a reference, is the address-of operator, or the binary AND.

What is the meaning of '*' and '&'?

This is possibly one of the most confusing things in Go. There are basically 3 cases you need to understand:

The & Operator

& goes in front of a variable when you want to get that variable's memory address.

The * Operator

* goes in front of a variable that holds a memory address and resolves it (it is therefore the counterpart to the & operator). It goes and gets the thing that the pointer was pointing at, e.g. *myString.

myString := "Hi"
fmt.Println(*&myString) // prints "Hi"

or more usefully, something like

myStructPointer = &myStruct
// ...
(*myStructPointer).someAttribute = "New Value"

* in front of a Type

When * is put in front of a type, e.g. *string, it becomes part of the type declaration, so you can say "this variable holds a pointer to a string". For example:

var str_pointer *string

So the confusing thing is that the * really gets used for 2 separate (albeit related) things. The star can be an operator or part of a type.

Meaning of *& and **& in C++

That is taking the parameter by reference. So in the first case you are taking a pointer parameter by reference so whatever modification you do to the value of the pointer is reflected outside the function. Second is the simlilar to first one with the only difference being that it is a double pointer. See this example:

void pass_by_value(int* p)
{
//Allocate memory for int and store the address in p
p = new int;
}

void pass_by_reference(int*& p)
{
p = new int;
}

int main()
{
int* p1 = NULL;
int* p2 = NULL;

pass_by_value(p1); //p1 will still be NULL after this call
pass_by_reference(p2); //p2 's value is changed to point to the newly allocate memory

return 0;
}

What does `*&` in a function declaration mean?

The & symbol in a C++ variable declaration means it's a reference.

It happens to be a reference to a pointer, which explains the semantics you're seeing; the called function can change the pointer in the calling context, since it has a reference to it.

So, to reiterate, the "operative symbol" here is not *&, that combination in itself doesn't mean a whole lot. The * is part of the type myStruct *, i.e. "pointer to myStruct", and the & makes it a reference, so you'd read it as "out is a reference to a pointer to myStruct".

The original programmer could have helped, in my opinion, by writing it as:

void myFunc(myStruct * &out)

or even (not my personal style, but of course still valid):

void myFunc(myStruct* &out)

Of course, there are many other opinions about style. :)



Related Topics



Leave a reply



Submit