0

I'm trying to understand the advantage of pointers. I know the definition and how to create one, but I still don't understand why they are powerful.

  • So can someone give me an example of something that you can only do with pointers.
  • I hear Python doesn't have pointers, does this mean that there are operations that can't be done using Python--I mean programming-wise, not just accessing a specific memory location and value? Examples?
stupidity
  • 437
  • 4
  • 7
  • 16

1 Answers1

0

Pointers are primarily a mechanism to implement reference semantics. If you want to refer to an existing object that lives somewhere else, then you can do so by passing a pointer to that object around.

Without pointers, you could only pass copies of objects around. This would allow you to communicate val­ues, but you couldn't modify an existing object. (Unless your language offered native reference hand­ling, like C++ does, of course.)

As a test, imagine how you would implement the following code:

var a = 10;
var b = 20;

my_magic_swap(a, b);  // how to do this?!

assert(a == 20 && b == 10);

(The answer is of course that my_magic_swap should accept pointers (or equivalent) to the original variables.)

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084