Thread: Pointers, a real explanation

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    20

    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. #2
    Banned
    Join Date
    Nov 2007
    Posts
    678
    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. #3
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    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.
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

  4. #4
    Banned
    Join Date
    Nov 2007
    Posts
    678
    [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. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    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.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  7. #7
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    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>
    
    
    int add(int b)
    {
        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
    0x004012f1 <add+1>:     mov    ebp,esp
    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
    0x004012f9 <add+9>:     pop    ebp
    0x004012fa <add+10>:    ret
    As you can see, it deferences a pointer to the variable and adds to it.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by IceDane View Post
    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>
    
    
    int add(int b)
    {
        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
    0x004012f1 <add+1>:     mov    ebp,esp
    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
    0x004012f9 <add+9>:     pop    ebp
    0x004012fa <add+10>:    ret
    As you can see, it deferences a pointer to the variable and adds to it.
    That's an incorrect conclusion - it dereferences EBP which is a copy of ESP, so it's "fetching b from an offset off the stack". If you turn on optimization (but not inlining), it will probably pass b as a register, and not dereference the stack.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 10:23 AM
  2. array of pointers to an array pointers
    By onebrother in forum C Programming
    Replies: 2
    Last Post: 07-28-2008, 11:45 AM
  3. delete and delete[]
    By Hunter2 in forum C++ Programming
    Replies: 13
    Last Post: 06-26-2003, 04:40 AM
  4. Pointers on pointers to pointers please...
    By Morgan in forum C Programming
    Replies: 2
    Last Post: 05-16-2003, 11:24 AM
  5. Pointers pointers pointers....
    By G'n'R in forum C Programming
    Replies: 11
    Last Post: 11-02-2001, 02:02 AM