Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Benefits of pointers?

Tags:

c

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?)

like image 801
Nick Avatar asked Dec 18 '10 23:12

Nick


4 Answers

One benefit of pointers is when you use them in function arguments, you don't need to copy large chunks of memory around, and you can also change the state by dereferencing the pointer.

For example, you may have a huge struct MyStruct, and you have a function a().

void a (struct MyStruct* b) {
   // You didn't copy the whole `b` around, just passed a pointer.
}
like image 172
alex Avatar answered Oct 18 '22 06:10

alex


Coming from Java, you'll have a slightly different perspective than what is presented in K&R (K&R doesn't assume that the reader knows any other modern programming language).

A pointer in C is like a slightly more capable version of a reference in Java. You can see this similarity through the Java exception named NullPointerException. One important aspect of pointers in C is that you can change what they point to by increment and decrement.

In C, you can store a bunch of things in memory in an array, and you know that they are sitting side by side each other in memory. If you have a pointer to one of them, you can make that pointer point to the "next" one by incrementing it. For example:

int a[5];

int *p = a; // make p point to a[0]
for (int i = 0; i < 5; i++) {
    printf("element %d is %d\n", i, *p);
    p++; // make `p` point to the next element
}

The above code uses the pointer p to point to each successive element in the array a in sequence, and prints them out.

(Note: The above code is an expository example only, and you wouldn't usually write a simple loop that way. It would be easier to access the elements of the array as a[i], and not use a pointer there.)

like image 21
Greg Hewgill Avatar answered Oct 18 '22 07:10

Greg Hewgill


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.

like image 5
EnabrenTane Avatar answered Oct 18 '22 05:10

EnabrenTane


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.

I'd say that this means that they are incrementing the pointer itself, which means changing the address (and therefore making it point to a different value). This could be useful if they were passed the first item of an array and want to continue in the array, but not change the values.

like image 4
zsalzbank Avatar answered Oct 18 '22 05:10

zsalzbank