# Thread: Pointers, a real explanation

1. ## Pointers, a real explanation

Ok,

let me start by saying I get it.. a variable that points to the address of a value another variable holds.

the real down fall in all texts and tutorials I've read is where you would actually apply this idea. I understand how you get aPtr to dereference int a....

it just doesnt make sense WHY you would do it. I have done a hundred (well maybe literally 20) that show how things point to each other, defreference, and alter the original a value.

but WHY damn it. what is a real world example. I wrote a rant thread about hating myVariable conventions. and this is why. it doesnt tell you anything. just like every thing describing pointers.

I learn all my stuff by relating it to something. but nothing tells you how and why it relates to an actual situation you would program with pointer.

} /* end of another rant */

2. simple example would do:
Code:
```void change_AB(int a, int b) {
}```
how do you think that the above function can change a, b?
there are many other uses, but this one came up just instantly

3. Consider a program to keep track of your CD collection. You would use structures to keep track of all the relevant data; artist, title, genre, etc. Then you would need a way to organize this list of CDs you have. A linked list is a natural way to do it, but to implement a linked list you need pointers.
Code:
```struct CD_record
{
char *title;
char *artist;
char *genre;
struct CD_record* next;
};```
Consider this struct. It contains the 3 data elements I mentioned, and a pointer to another CD_record. I can now malloc memory to create an individual record. The first record in our list is pointed to by something like
Code:
`struct CD_record *head; /* head of the CD_record list */`
and the next pointer is set to NULL. When we find a record in this list that points to NULL as the next, then we know that record is the last one in the list.
As we add records, we malloc more memory for each record and then set the next pointer of the end of the list to point to this new record we just added.
If we want to remove a record, we change that next pointer of the previous record in the list to skip over the record we are going to delete (1 points to 2 which points to 3, if we want to delete 2 we make 1 point to 3) and then free() the unwanted record.
This is a common data structure that would be difficult without using pointers. You can traverse the list, for example to search for a particular record, by starting at the head of the list and continuing to move to the next record while (current_record->next != NULL). If the current_record->title is the title you are looking for, you can break out of the while loop and current_record points to the record you were looking for to work with.

Binary Search Trees (BST) are another data structure that is commonly used and would be insanely difficult (if possible at all) without using pointers. A BST uses pointers sort of like a linked list and allows faster searching through the data than a linked list would allow.

These are just 2 examples off the top of my head. I understand your frustration since I remember when I started learning C and just couldn't wrap my head around pointers. When I finally did I still didn't know what they were useful for, but as I learned more about programming and data structures, I began to learn where the pointer's place is in the world.

The fact that you understand what a pointer is will help you tremendously when you start learning about something that requires pointers. The C book I first used taught linked lists before really going into detail about what the pointers were, so I couldn't understand that either. Linked lists became clear when I finally understood pointers.

4. [Jessy, not playing guitar? ]
I had a data structure book that had examples of implementing all the data structures without using pointers (mainly because, the book was not based on C, it was using plain pseudo code).

pointers are not difficult (unless something really complex is being done, in which case, every variable declared becomes, head-ache) at all, may be you just need a good explanation that is best suited to your way of thinking.

5. something that I didn't see mentioned on the list at the above link is the fact that it is nearly impossible to perform any file I/O without pointers.

6. Well, there's the performance factor for the first part. Imagine, if every time you want to pass data to a function, you have to pass it by value. That is, copy it. Imagine that you have a HUGE array and you want to pass it to a function, and it copies it directly.

Pointers are just ways to access data that might or might not be outside the function's stack frame. Or, in the case that it's dynamically allocated, it'll be on the heap.

Either way, your programs use pointers constantly. It's the way things work. Data is located somewhere in memory, and to get it, you need the memory's address(Or to write to it, for that matter).

This code

Code:
```#include <stdio.h>

{
b++;

return b;
}

int main(void)
{
int crap = 2;

add(crap);  // Variable won't be modified, just an example.
}```
disassembles to this code on my machine(Vista, Mingw)

Code:
```0x004012f0 <add+0>:     push   ebp
0x004012f3 <add+3>:     inc    DWORD PTR [ebp+0x8]         ;; DEREFERENCING A POINTER
0x004012f6 <add+6>:     mov    eax,DWORD PTR [ebp+0x8]     ;; Return value is always in eax
As you can see, it deferences a pointer to the variable and adds to it.

7. Originally Posted by IceDane
Well, there's the performance factor for the first part. Imagine, if every time you want to pass data to a function, you have to pass it by value. That is, copy it. Imagine that you have a HUGE array and you want to pass it to a function, and it copies it directly.

Pointers are just ways to access data that might or might not be outside the function's stack frame. Or, in the case that it's dynamically allocated, it'll be on the heap.

Either way, your programs use pointers constantly. It's the way things work. Data is located somewhere in memory, and to get it, you need the memory's address(Or to write to it, for that matter).

This code

Code:
```#include <stdio.h>

{
b++;

return b;
}

int main(void)
{
int crap = 2;

add(crap);  // Variable won't be modified, just an example.
}```
disassembles to this code on my machine(Vista, Mingw)

Code:
```0x004012f0 <add+0>:     push   ebp