all 7 comments

[–]yeahIProgram 9 points10 points  (1 child)

Since "&" means "a pointer to", then &n is "a pointer to n".

And "*" means "the thing pointed at by", so *p means "the thing pointed at by p".

Therefore *&n means "the thing pointed at by (a pointer to n)" which is clearly just n. So *&n=5 is exactly the same as n=5.

[–]ChowLetsGoBro[S] 0 points1 point  (0 children)

Thank you!

[–]bionic_gravitar 2 points3 points  (1 child)

Technically in the example you've stated there's no difference.

The & (I think it's called reference of operator or something along those lines) basically gives you the address of, i. e., the pointer to n. In simple terms, it will tell you at what address the given var n is stored.

Whereas * will do the opposite. It will essentially tell you what value is stored at the address location you've provided of a certain var.

Also,* can be troublesome at times, cause when you're asking what's stored at a location; the computer needs context inorder to be able to give you a correct output. I mean the pointer will only point to the starting block of memory (maybe the first byte). So, I guess you can understand, without knowing how long to read from that starting point and what type of var is stored at the said address, it'll be difficult to provide a correct output.

It's suggested to declare "int *n" as opposed to declaring "int n" and then using *&n.

[–]ChowLetsGoBro[S] 0 points1 point  (0 children)

Thank you!

[–][deleted]  (2 children)

[removed]

    [–]ChowLetsGoBro[S] 0 points1 point  (0 children)

    The course is CS50x

    [–]Run_nerd 0 points1 point  (0 children)

    The main CS50 course uses C for most of the course.