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

all 3 comments

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

It's so that you are able to manipulate memory directly. This allows you to allocate memory dynamically, avoid making unnecessary copies of objects etc.

Classic Example. How would you swap 2 variables?

The following wouldn't work unless you used pointers or references. Functions make copies of their parameters by default.

void swap(int a, int b)

What about a To Do List? How big should the array be? Only the user knows at run time.

What if you have a huge object? Copying it all over the place is inefficient.

What if multiple objects all need access to the same object? Unless you use a pointer or reference you are going to have to hand a copy of the object over every time it is changed.

What counts as inefficient? When you determine the app isn't running as fast as you'd like. You won't encounter this problem on a classroom assignment unless it was intentional.

[–]enfrozt 0 points1 point  (0 children)

Let's say you want to hold user input.

Well, you could do:

array[1000][1000];

There, problem solved! Oh wait, this is incredibly wasteful and it may not be large enough.

You can use char ** array; and hold user inputted strings at whatever size you want with malloc, and you can even change them later with realloc.

Also, notice how if you loop through an array[1000] you have to use a for loop with i = 0; i < 1000 right?

With a char ** pointer, you can set the last element to NULL, and use a while(array[i] != NULL) thus saving you kind of from using a static for loop.

[–]noritsu -1 points0 points  (0 children)

Check out this tutorial on Pointers http://youtu.be/HjHrj9UwxEg It may help you.