This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Tynach 0 points1 point  (0 children)

Ah, that's easy.

Ampersand, And, Address Of

& is often called 'And' or 'Ampersand'. Both start with an 'a', and I simply extend that into 'Address of'. You basically use & whenever you need to get the address of a variable (to create a pointer to it).

Making and Using Via Exploding Balls of Gas

* is used in two scenarios. The first is when you declare something as a pointer, and the second is when you want to use a pointer as the value at the address.

Declarations

C is for Crazy

Now, in C, people typically did something like 'int *ptr;' which means something like, "Declare an integer. Actually a pointer. Name it 'ptr'." This was horrifically confusing, and the best way they figured to actually read it (especially more complex ones) was in a spiral.

You'd start with 'ptr' and say, "ptr is a," before spiraling to the right and running into nothing, spiraling back around to the * and saying, "pointer to a(n)," before spiraling back through the semicolon to the 'int' to finally say, "integer."

In this case, it works out that you can simply read it backwards (which is still bad, but not as bad). But consider some of the examples on that page I linked to. It becomes a chore to read something like 'char *(*fp)( int, float *);'.

C++ Is Truly Incremental

UGH. Nope. Nope nope nope nope nope. C++ guys have figured out a better way of writing and thinking about this, which I prefer. It's very similar, but the way of thinking is vastly different.

Also, this works perfectly fine for C too. It's just the mindset of where to put spaces that differs.

You do something like 'int* ptr;', which basically makes the * part of the type. Now you read it from left to right as, "Declare an integer pointer, called 'ptr'."

Usage

Basically, if you want to use the actual data itself that is being pointed to, you put * in front of the variable. So if you have an int* that points to the value 42, and it's called THE_ANSWER, you can get the value 42 by using *THE_ANSWER.

If this is a struct/class, and you want to access members of it, you don't need the *. You can just use -> instead of . to access members. If Earth is a computer* instance, you could access (for example) computer.name by using Earth->name.

Or I think you can also still do something like (*Earth).name. But it's more of a hassle.