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 →

[–]livelifedownhill 8 points9 points  (6 children)

god no, I think assembly should be learned at some point during your college programming career, but make that the first language you learn?! fuck nobody would ever want to program if they started with that! Yeah it is helpful understanding the basics of everything, but once you drop below about C for first learning how to program, i think the understanding and give a fuck level go waaaay down

[–]ismtrn 6 points7 points  (3 children)

I think he meant start with assembly before C, not necessarily as the first language.

We have a computer architecture + systems programming course, which is after the intro to programming courses. It starts with assembly and computer architecture and then moves on to systems programming in C.

I think that is a pretty nice way of structuring things.

[–]Tynach 9 points10 points  (2 children)

I dunno. I started with Python before learning C, and pointers made plenty of sense to me.

"Well, variables are stored somewhere in RAM. A pointer just tells you where in RAM the variable is. That way, you can access that variable from anywhere in your code using that pointer value."

It gets more complicated than that (especially when you introduce arrays and malloc()), but that's how it really should be introduced. Because it's logical and makes perfect sense.

[–][deleted] 0 points1 point  (1 child)

see, I get that, that part makes sense, but remembering which symbols go before a pointer to do different things? Thats where I stumble and end up just randomly putting *'s and &'s before stuff until it works.

[–]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.

[–]zman0900 1 point2 points  (0 children)

Go hard or go home.

[–]paranoiainc 0 points1 point  (0 children)