Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In C++, what is the difference between:

 void func(MyType&); // declaration

 //...
 MyType * ptr;

 func(*ptr); // compiler doesnt give error

 func(ptr); // compiler gives error i thought & represents memory address so 
            // this statement should correct as ptr is only a pointer
            // or address of some real var.
like image 984
PUG Avatar asked Jul 28 '10 07:07

PUG


People also ask

What is the meaning of & *?

The word ampersand is a mix of Latin and English and a condensed version of the phrase "and per se and," which means "(the character) '&' by itself is 'and'." Definitions of ampersand. a punctuation mark (&) used to represent conjunction (and) type of: punctuation, punctuation mark.

What does 42 mean in texting?

"The Answer to Life, the Universe and Everything (from Hitchhiker's Guide to the Galaxy)" is the most common definition for 42 on Snapchat, WhatsApp, Facebook, Twitter, Instagram, and TikTok. 42. Definition: The Answer to Life, the Universe and Everything (from Hitchhiker's Guide to the Galaxy)

What does :> mean in texting?

means "Angry."

What does * ___ * mean?

"In Love" is the most common definition for *_* on Snapchat, WhatsApp, Facebook, Twitter, Instagram, and TikTok. *_* Definition: In Love.


1 Answers

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.

like image 101
sbi Avatar answered Oct 07 '22 06:10

sbi