Nick Nikolaou Nick Nikolaou - 1 year ago 121
C Question

Benefits of pointers?

I recently developed an interest in C programming so I got myself a book (K&R) and started studying.

Coming from a University course in Java (basics), pointers are a totally new chapter, and from what I read online it's a rather difficult concept to get your head around. Before getting to the pointer chapter I was under the impression that pointers are a major part of C and provide great benefits.

Upon reading the chapter and getting a basic idea of what pointers are and how they work, the benefits are not obvious to me.

For example (please correct me if I got this totally wrong) in the introduction of pointers in the K&R book it says that since we call by value, when passing a variable in a function call we pretty much pass a copy of the variable for the function to handle and therefore the function can't do anything to the original variable and we can overcome this with pointers.

In a later example that uses a char pointer, the book says that incrementing the char pointer is legal since the function has a private copy of the pointer. Aren't 'private copies' a reason to use pointers instead?

I guess I'm a bit confused on the whole use of pointers. If asked I can use pointers instead of using array subscripts for example, but I doubt this is the main use of pointers.

Linux and Open source programming was the main reason I got into C. I got the source code of a C project to study (Geany IDE) and I can see that pointers are used throughout the source code.

I also did a bit of searching in the forums and a found a couple of posts with similar questions. An answer was (I quote):

If you don't know when you should use pointers just don't use them.

It will become apparent when you need to use them, every situation is different.

Is it safe for me to avoid using pointers at the time being and only use them in specific situations (where the need for pointers will be apparent?)

Answer Source

Your highlighted rule is very wise. It will keep you out of trouble but sooner or later you have to learn pointers.

So why do we want to use pointers?

Say I opened a textfile and read it into a giant string. I can't pass you the giant string by value because it's too big to fit on the stack (say 10mb). So I tell you where the string is and say "Go look over there at my string".

An array is a pointer ( well almost ).

int[] and int* are subtly different but interchangeable for the most part.

int[] i = new int[5]; // garbage data
int* j = new int[5]   // more garbage data but does the same thing
std::cout << i[3] == i + 3 * sizeof(int); // different syntax for the same thing

A more advanced use of pointers that's extremely useful is the use of function pointers. In C and C++ functions aren't first class data types, but pointers are. So you can pass a pointer to a function that you want called and they can do so.

Hopefully that helps but more than likely will be confusing.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download