C ++: Asterisks, Ampersand and Star?

I recently came across this video on Paradigm Programming and prof. such as Asterisks, Star and Ampersand.

Here's how he used these operators:

int i = 37; float f = *(float*)&i; 

And as he voiced line 2 when writing:

Float "f" equals a floating-point asterisk, ampersand i

I understand what asterisk and ampersand mean, but what is the significance of using a star here? Is it a synonym for an asterisk?

+6
source share
4 answers

* float used to form the type. Very often, referring to the type of pointer in words, people will say “star” after type “pointer” type, for example. "malloc returns an empty star." I have never heard anyone use an asterisk to indicate a type.

* at the beginning it is used to cancel the reference to the pointer, thereby referring to the value indicated to it (interpreted as float due to the following cast). Again, from my own experience, I never heard anyone use a "star" here. People simply tend to say “de-link” to describe the operation being performed.

I would not read too much about this. There are two different contexts here that you correctly noticed, and as long as you understand what they mean in terms of C ++, then that’s fine.

+8
source
 float f = *(float*)&i; 

In this case, left * and right * have different semantics. Left * means the value obtained by reference. Right * declares a reference type.

+2
source

Is it a synonym for an asterisk?

Yes. Shift + 8 on MY Windows keyboard. Your example demonstrates why you should not try to read a character symbol in C ++ symbol. "Equal" in C ++ is "==". "=" is an assignment. Plus, he forgot to talk about parentheses and commas. At this point (4 errors in one line of code), he could write this damn thing silently.

It would be much better if the dude read this part:

 float f = *(float*)&i; 

like that:

"Take a pointer to i, C-style, enter it into a pointer to float, dereference and assign the value to float f." It makes much more sense that "star / asterisk" is gibberish.

PS If you really love language chests, you can try reading aloud any code that uses boost, iostreams, operator <, casts, bitwise operations and the "camel case" to distinguish between classes and methods. Of course, such an exercise will not improve your programming skills ...

+1
source

I think the host just wanted to avoid the awkward silence while he wrote the code. As far as I know, in this context there is no difference between an asterisk and a star.

0
source

Source: https://habr.com/ru/post/912680/


All Articles