A pointer can be a value stored in memory (or in swap space or in a register).
A pointer is a variable whose value is an address in memory. This graph shows you the contents of "pointer".
Code:
+----------------+
| 0x3f93f84e | -------------------------> Memory location at 0x3f93f84e
+----------------+
int *pointer;
Then programmers say that pointer "points to" the thing that is at memory address 0x3f93f84e.
If you say *pointer, you mean you wanna access whatever is at memory location at 0x3f93f84e. Since you meant pointer to be a pointer to _int_ (int *pointer is a * (pointer) to int), the computer fetches an _int_eger from memory location 0x3f93f84e.
Code:
+----------------+
| 0x3f93f84e | -------------------------> Segmentation Fault
+----------------+
printf("%d\n", *pointer);
What happened? That's strange! But the solution is easy. The pointer is uninitialized. That means, it's value is undefined (probably not what you want). Since the space where pointer is stored (pointers are also stored in memory) has probably been used before, it will contain some crap left from other programs or your own program. This crap is 0x3f93f84e. The computer doesn't know that, it makes just what you want - in this case, it fetches an integer from somewhere in you memory.
BUT - That's a security risk. What if 0x3f93f84e points to operating system data or *where your passwords are stored in*? Then any hacker can get into your computer by accessing anything he wants. He makes the pointer "point to" somewhere he wants, for example where your login name and password are stored. Then he accesses that location. Voila, he has you name and password, if they are not encrypted (don't worry about this part).
Or say, you want to modify *pointer. You could write zeros there where your operating system's heart is beating. That would immediately crash your computer. Either you didn't want this to happen, or a hacker wanted to kill your session. Too late. You've lost your 1000 line program cuz you didn't save.
To prevent this from happening, the operating system doesn't allow you to look at position 0x3f93f84e in memory. It neither allows you to write there. For your own security. That's, PAY ATTENTION where you pointers point to.
Code:
+----------------+ +----------------+
| 0xac398ef4 | -------------------------> | 0x00000000 |
+----------------+ +----------------+
pointer = &i; int i = 0;
Now you say, "make pointer point to i", or "gimme the address of i and save it in pointer". The '&' operator takes the address of something, for example i. It returns 0xac398ef4 in this case, which is the memory location where i is saved in. So the value of pointer is the address of i.
(Of course, we have assigned i to zero, that means in fact we have written all zeros into the location where i resides)
Now, let's take a look at i. We could simply write "printf("%d\n", i)". But that's boring! We wanna learn something. Ok, let's try to use pointer. Remembered the part with the Segmentation Fault? Try again!
Code:
+----------------+ +----------------+
| 0xac398ef4 | -------------------------> | 0x00000000 |
+----------------+ +----------------+
printf("%d\n", *pointer); i
Compile... Run... YESSS! No strange errors, just plain zero. i was zero, remember? Now we get the same zero! What happened? We wanted to fetch an integer from memory location 0xac398ef4. Now we know that we can read from there, cuz it's *our* memory and nobody else can read from there except we can. We own i, so we own 0xac398ef4 where i is stored. Congrats, for you own some memory now!
But what if we want to put a 5 into i? "i = 5;", right? BOORING! We can do it like that:
Code:
+----------------+ +----------------+
| 0xac398ef4 | -------------------------> | 0x00000005 |
+----------------+ +----------------+
*pointer = 5; i
Now we say "write a 5 wherever pointer points to!". Since pointer points to i, we write 5 into i. Now, test i:
Code:
+----------------+ +----------------+
| 0xac398ef4 | -------------------------> | 0x00000005 |
+----------------+ +----------------+
printf("%d\n", *pointer); i
Compile... Run. What do we see? We printed out *pointer first remember?, that gave us a zero. Now, we write a 5 into the location where i lives. Now, we print out i... The output is... 5! Wow! Cool, huh? If we print *pointer again, we'll get another 5. If you assign i to 293, *pointer will give you 293. It's just always the same number.
That was the basics. Anyone who doesn't understand can tell me. Don't be ashamed, I'm sure probably _nobody_ here knew it since s/he was a baby. But once you get it, you know very much. Pointers are one of the REAL powers of C.