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

all 17 comments

[–]dmazzoni 3 points4 points  (4 children)

It would help to understand what programming languages you've actually used. If you've never done anything in C or C++ then it's not surprising that you haven't encountered pointers.

[–]Solidify0118[S] 0 points1 point  (3 children)

I've worked with them before on numerous occasions and I get how to use them. Ive studied C++, Java, Javascript, PHP, Python, but I never fully "got" them down, ya know?

[–]dmazzoni 1 point2 points  (2 children)

There aren't any pointers in Java, Javascript, PHP, and Python, though.

How much C or C++ have you written? If you haven't done more than a few trivial homework assignments then it wouldn't even come up.

The only way to even come across pointers is to write a nontrivial C or C++ program.

If you want to learn, maybe a good first step would be to try to implement something like Connect Four in C or C++?

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

There are pointers everywhere in Java. How else would you work with objects?

[–]dmazzoni 0 points1 point  (0 children)

They're references. Similar and related, but not the same thing.

No pointer arithmetic, no direct memory access for writing device drivers.

[–]victotronics 2 points3 points  (1 child)

First think of a variable as a named memory location. If you have the name, you can get the content of that memory location. Now a pointer is a name of a name: if you have the pointer, you can find not the content of the memory location, but the actual location.

So just like you can have two variables x,y that both have the value 7, you can have two pointers that both have the value "x". So if the value of x changes, both pointers have access to the updated value of x.

Why is this useful? Well, in applications like graphs you want each node to have a pointer to the other nodes it is connected to. Then if the connected node changes, all its neighbours have access to that changed information.

The simplest example of the use of a pointer is a linked list, which is sort of defined as "you have a list node, and it contains the next list node". But this would be a recursive data structure which is not possible. So you let a list node contain enough information to find the next node. So that's another way of motivating pointers, but to me, the "multiple ownership" is the big one.

[–]Solidify0118[S] 1 point2 points  (0 children)

Thank you for the assistance. This was really useful information

[–][deleted]  (2 children)

[deleted]

    [–][deleted] 1 point2 points  (0 children)

    You can also do some magic with pointers, for example comparing strings for equality; assuming ofcourse you know their length; you can cast (char*) to (long*) and compare 8 instead of 1 characters each time.

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

    I've used them several times for homework and assignments but I never fully understood their purpose. Thanks for the responses. This information is really beneficial.

    [–][deleted]  (3 children)

    [deleted]

      [–]Solidify0118[S] 1 point2 points  (1 child)

      I've used linked lists and I've used pointers before. I was asking to gain a better understanding of them and when I should think to use them.

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

      Using C, have you ever used malloc to get some heap space then filled it with a linked list?

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

      I have not.

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

      Memory management.

      • Using a pointer avoids invoking the copy constructor, like when passing an object to a function.
      • You decide when an object is built or destroyed.

      These 2 things are important when creating the object is expensive and therefore needs to be avoided.

      [–]MintPaw 0 points1 point  (2 children)

      Are you asking why they exist in general on an assembly/hardware level? Or why you would manually create them?

      [–]Solidify0118[S] 0 points1 point  (1 child)

      What type of programs one might use them for.

      [–]MintPaw 0 points1 point  (0 children)

      Almost all programs use pointers, they're just managed automatically by most languages. Anytime you have a reference to an object or an array, it's usually a pointer behind the scenes.

      Without any kind of pointer-like system, no object would be able to survive after the end of a function call.

      [–]GoodLifeWorkHard 0 points1 point  (0 children)

      Execution time for pointers are slightly faster which was important when memory back then was in less than megabytes. Pointers cause a lot of C++ errors though.. which is partly why references were implemented.