Pointers have commonly been given a bad reputation, this is because it's hard to see what pointers are for at first. Unfortunately, when you don't understand why something is used the less motivated you are to learn them. That being said, they are actually very useful tools so if you stick around you'll be happy you did. Here is the syntax for a pointer.
int *p;
This is how you declare a pointer, notice how it isn't all that different from declaring just a normal variable only you must put and asterisk in front of the variable. We have just declared p to be a pointer and p must point to an integer hence the int before *p. So how do we get p to point to something? Easy we give it an address.
p=&x;
P now points to the address of x. This is exactly what pointers do best, they hold the addresses of other variables. So whatever you do with *p, x will do the same. For example lets initialize *p to a value.
*p=5;
Can you guess what x is? Yes! 5. Because *p now points to x we can manipulate x just by manipulating *p. Lets see a basic program using a pointer.
#include <stdio.h> #include <stdlib.h> int main() { int x; int *p /*Declaring a pointer called p*/ p=&x; /*p now points to x*/ *p=6; /*Initialize the pointer to the value of 6; printf("%i",x); /*print out the value of x*/ system ("pause"); return 0; }
This program will print out whatever you intialize to *p. So you may already be asking yourself, instead of manipulating *p, couldn't we have just changed x directly. Yes, and this is where it can be confusing at first, but this example program doesn't show what pointers are necessarily for, this program is to show you how pointers work. The main reason for pointers is dereferencing data, instead of changing a variables value you change its address, why? In c programming when it comes to functions you cannot return a variable whose value has changed from within the function, but you can change it's address! First let us take a look at a function that does NOT use pointers and attempts to change a value of a variable from within the function.
#include <stdio.h> #include <stdlib.h> int add_5(int n); /*Remember you must prototype the function before you use it*/ int main() { int x; scanf("%i",&x;); /*scan in a value for x*/ add_5(x); /*call the function and send it the value of x*/ printf("%i\n",x); /*prints out the value of x*/ system("pause"); return 0; } int add_5(int n) /*n now holds the value of x*/ { n=n+5; /*n equals what it was plus 5*/ return n; /*return n back to x/* }
What this program does is simple, scan in a value of x, send it to a function that takes that value adds five and returns it. However, if you compiled and ran this program you would notice all it does is print out what you already entered, for example, if you entered 5 it would return 5 when you should be expecing 10 right? This is precisely what pointers are for, we cannot change the value of x, so we change it's address by using pointers. Now we will look at another program that is identical to the last only it is uses pointers.
#include <stdio.h>
#include <stdlib.h>
int add_5(int *p); /*prototype your function, this time we are using a pointer called *p */
int main()
{
int x;
int *p; /*you must declare your pointer, the pointer is called p and it is going to point to an integer*/
scanf("%i",&x;);
p=&x; /*p now points to x*/
add_5(&x;); /*careful on this one! Call the function add_5 and send it the address of x*/
printf("%i\n",x);
system("pause");
return 0;
}
int add_5(int *p) /*define the function*/
{
*p=*p+5; /*pointer p equals what it was plus 5*/
return *p; /*return the pointer p*/
}
When you run this program you will see it works just how we wanted it to. The program takes in a value and returns the value plus 5. This is all thanks to pointers! Pointers are also very useful when using arrays. The next section will go over arrays but not how they are used with pointers, these lessons are intended for beginners and designed for someone new to programming to simply get a feel of what it is all about.