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 a
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.
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.
}
Let me explain this more in terms of Java references (as pointed out by @Greg's answer)
In Java, there are reference types (i.e. reference to a class) and value types (i.e. int
). Like C, Java is pass by value, only. If you pass a primitive type into a function, you actually pass the value of the value (after all, it is a "value type"), and therefore any modifications to that value inside that function are not reflected in the calling code. If you pass a reference type into a function, you can modify the value of that object, because when you pass the reference type, you pass the reference to that reference type by value.
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.
Considering that you're coming from a Java background, here's the simplest way to get your head around what use pointers have.
Let's say you have a class like this in Java:
public class MyClass {
private int myValue;
public int getMyValue() { return myValue; }
public void setMyValue(int value) { myValue = value; }
}
Here's your main function, that creates one of your objects and calls a function on it.
public static void Main(String[] args) {
MyClass myInstance = new MyClass();
myInstance.setMyValue(1);
System.out.printLn(myInstance.getMyValue()); // prints 1
DoSomething(myInstance);
System.out.printLn(myInstance.getMyValue()); // prints 2
}
public static void DoSomething(MyClass instance) {
instance.setMyValue(2);
}
The myInstance
variable you declared in Main
is a reference. It's basically a handle that the JVM uses to keep tabs on your object instance. Now, let's do the same thing in C.
typedef struct _MyClass
{
int myValue;
} MyClass;
void DoSomething(MyClass *);
int main()
{
MyClass myInstance;
myInstance.myValue = 1;
printf("i", myInstance.myValue); // prints 1
MyClass *myPointer = &myInstance; // creates a pointer to the address of myInstance
DoSomething(myPointer);
printf("i", myInstance.myValue); // prints 2
return 0;
}
void DoSomething(MyClass *instance)
{
instance->myValue = 2;
}
Of course, pointers are much more flexible in C, but this is the gist of how they work in a Java sort of way.
If you are dealing with dynamically allocated memory, you have to use pointers to access the allocated space. Many programs deal with allocated memory, so many programs have to use pointers.