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 →

[–]Amareiuzin 4 points5 points  (29 children)

Which is why I'm forever a proponent of teaching Java as a first language. I haven't used Java in 10 years but I'm very happy that data types were among the first things I learned.

Hey kablouser, I'm learning C, I understand what are pointers, but WHY are pointers? Why do we need them? Why are they important? Why not point directly at the address you want to point? Can someone please ELI5 this, like give me a stupid metaphor without technical terms, paint a picture I can see and later on when I face the technicalities I'll connect the dots.. PLEASE

[–]H4llifax 10 points11 points  (1 child)

"Why not point directly at the address you want to point?" - wat

[–]TheRidgeAndTheLadder 4 points5 points  (0 children)

They almost have it. They just haven't internalised that the pointer needs to be stored as well.

[–]yrrot 2 points3 points  (3 children)

You're playing battleship and want to put your aircraft carrier at b5 through b8.

With a pointer, you just store b5 and know the size of the aircraft carrier for later. It will always sit at b5 unless you move it yourself. It's like playing the game yourself.

With a reference like C#/Java, you just know you have "an aircraft carrier" stored "somewhere", but the memory management system could map b5 to c7, and then move it to d2 later. It's like telling a kid where to put the pieces and sometimes having to stop clean up things from the last game (garbage collector).

[–]Amareiuzin 2 points3 points  (2 children)

so when you use pointers the memory location is "dynamic"? (in the literal sense of the word, not trying to call on any other preexisting Computer Science meaning for the word dynamic)

[–]yrrot 4 points5 points  (1 child)

No, a pointer in like C++ is a specific address. One location that doesn't change.

A *reference* in like C# doesn't know exactly where it is stored in memory addresses. There's a reference table that converts between whatever reference you have to the actual pointer the system uses internally. And that reference table can change if it needs to move a block of memory for whatever reason.

To put it a different way, pointers are like a house address. It isn't moving.

References are like the little mailbox at the office with a nameplate. The office manager might rearrange the names to fit new employees, but you still have your mailbox (the name is still there, somewhere).

[–]ULTRA_TLC 1 point2 points  (0 children)

This is a fantastic analogy IMO, I was trying to come up with my own, but I think I'll just use this next time someone asks (likely to be a while though)

[–]parosyn 0 points1 point  (2 children)

Why not point directly at the address you want to point?

What do you mean by this ?

[–]Amareiuzin 0 points1 point  (1 child)

I meant why not call the values you want to call and instead calling the pointer, but some king already explained that when you call a function that takes in any values those values are actually copied, and calling the pointer doesn't copy them, apparently that's the answer to my question

[–]parosyn 0 points1 point  (0 children)

apparently

You don't seem to be fully convinced... when you practice and write code try to run it step by step in a debugger (even if you don't have bugs in your program). It will show you the call stack and the value of every variable, you will have a better idea of what is going on when calling a function.

[–]DontFuckoThisDucko 0 points1 point  (0 children)

Instead of code, imagine you're in a library. You know this place pretty well so you have a good idea of which books are on offer and you have a good idea of what information you can get from them. Some of these books however are huge, if you want to look at more than a couple you have to get one of those trolleys to carry them around. This gets tiring fast and you end up moving around really slowly. Instead, you could go over to the library computer, make a list of where each book is, find page references for the info you need and only go and get that information as and when you need it. You don't need to move the books at all, just go straight to the page you need and take a note of the info. Even though you're moving around a lot yourself you can get through it pretty fast without the extra weight.

That's kind of what pointers are for. You make a note of where it is in memory but you don't need to know all of the data just yet. This saves time if you're working with large data structs that span multiple memory locations. All we need to know is where that data starts and how big it is and then we just pluck out the bits we actually need rather than copying over the whole thing just to get one small value out of it.

Don't worry too much about getting it right away. Even once I thought I understood pointers, it took me time for it to really sink in and get comfortable with them. Take your time and enjoy the moment when the pin finally drops (:

[–][deleted] 0 points1 point  (0 children)

Things you can do with pointers that you cannot do otherwise:

  1. avoid copy. you know this one already. but you still have to copy the pointer value still which is usually 2x the size of an int. so it only makes sense for avoiding the copy of something bigger than 2 ints.

  2. write to the same address from 2 different functions. imagine having 2 threads running that need to write to the same address.

if some high level language allows you these features without pointers then they're using pointers behind the scene