Thread: Pointers

  1. #1
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209

    Pointers

    If I understand this all correctly, pointers are like a shortcut on the desktop. I understand they point to something.

    However, why are they more useful ? Do they take up less memory ? Please don't say they're quicker, cuz it's not true, they take up an extra line to declare than normal variables. Why are they better ?

    And is there anything I should know about them that's very important ?

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Pointers and regular variables are VERY similar, you can make one look like another via derefrencing. Eg...

    int areg = 10;
    int *ptr = &areg;
    int breg = *ptr;

    Pointers ARE faster. Instead of passing an entire-- say, 32b structure to a function, why not pass a 4b pointer? It's much faster.

    If you don't understand them, or see a need for them right now-- don't use them. For me, pointers were the hardest concept to understand... but today I couldn't code without them. Eventually pointers will work their way into your toolbox.

    Try reading the linked list tutorial on this website. www.cprogramming.com/tutorial.html then see if you can come up with a way to do it NOT using pointers.

    This is one link that helped me understand pointers a little better:
    http://www.augustcouncil.com/~tgibson/tutorial/ptr.html

  3. #3
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    int breg = *ptr;

    Does this declare a pointer ? Any what do you mean by a 32b structure / 4b ?

    Thanks for the tutorial, reading that now.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Pointers are extremely useful. Perhaps you haven't studied them enough to realize their usefulness, but in time it will become apparent. Here are some small examples:

    Code:
    char player[] = "Player";
    char computer[] = "Computer";
    char neither[] ="Neither";
    char * winner = neither;
    //...play a game
    if(a>b)
    winner = player;
    else
    if(b>a)
    winner = computer;
    else
    winner = neither;
    //...more code
    cout << winner << " wins the game." << endl;
    In this example, a pointer was used to allow a single print statement as opposed to many. Thus pointers are great for keeping a "current selection" variable.

    Code:
    char str[] = "There are many uses for pointers.";
    char search[] = "pointer";
    char * found = strstr(str, search);
    if( !found ){
     cout << "Word \"" << search << "\" was not found." << endl;
    } else {
    int position = found - str;
    cout << "\"" << search << "\" found, starting at position " << position << endl;
    }

    Here we made use of the fact that arrays store their contents in increasing addresses. Thus the length was found by subtracting the higher address from the base address.

    Also, you can see that setting a pointer to NULL can signify that something was not found, an often used trick.



    Pointers are faster to pass as parameters to functions than large stuctures. When you pass a large structure to a function, a replica is made, which takes time and memory. Had you passed a pointer to it, the function would've used that 4 byte address and and thus wouldn't have replicated the data.



    Honestly, there are hundreds of uses for pointers, and I'm sure you'll realize that soon enough!

    Hope that helps.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Originally posted by Korhedron
    int breg = *ptr;

    Does this declare a pointer ? Any what do you mean by a 32b structure / 4b ?

    Thanks for the tutorial, reading that now.
    No, you're declaring an integer and assigning it the value that ptr points to. Consider this:

    Code:
    int number = 25;
    int * n;
    n = &number;
    cout << number << endl; 
    *n = 50; 
    cout << number << endl; 
    *n = 75;
    cout << number << endl; 
    *n = 100;
    cout << number << endl;
    So by dereferencing 'n' we change the value of number.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Sebastiani, thanks for your examples, the first one helped, but I didn't understand all of the second one...

    Does

    int breg = *ptr

    declare and integer AND a pointer and say that that integer points to that pointer ?

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    No, you can't declare a variable on the right side of an '=' sign. That code just declares an integer, and then assigns it the value of the variable that 'ptr' (which was already declared) points to.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Ah, I understand now, thanks a lot.

    Could you explain the

    Pointers ARE faster. Instead of passing an entire-- say, 32b structure to a function, why not pass a 4b pointer? It's much faster.
    thing that Eibro posted ? What's the 32b, is it 32 bits and what does the thing mean ? Sorry about this... I ordered a good book, but until then...

  9. #9
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    32 bytes / 4 bytes. A pointer is always 4 bytes in 32 bit environments, as it's an integer which holds a memory address.

    Consider this:
    Code:
    struct book {
       double isbn;
       int price;
       int pages;
    };
    
    // an object of type book would be 12 bytes large (3 * 4, assuming an integer is 4 bytes)
    
    void print_book(book bookprint)
    {
       cout << bookprint.isbn;
       cout << bookprint.price;
       cout << bookprint.pages;
    }
    
    void p_print_book(book *bookprint)
    {
       cout << bookprint->isbn;
       cout << bookprint->price;
       cout << bookprint->pages;
    }
    
    book opengl_superbible = {1571691642, 75, 696 };
    print_book(opengl_superbible); 
    // copy the entire opengl_superbible structure (12 bytes), push it onto the stack and call the function.
    
    p_print_book(&opengl_superbible);
    // push the address of opengl_superbible onto the stack (4 bytes) and call the function.
    Which do you think is faster?

  10. #10
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Guys, I'm sorry, I didn't understand half of that...

  11. #11
    still a n00b Jaguar's Avatar
    Join Date
    Jun 2002
    Posts
    187
    Simpliest...
    1 byte contains 8 bits.
    So in 32 bits environment, there must be 4 bytes per one memory address.
    Pointer contains memory address, so one pointer consumes 4 bytes.
    You can compare shotcut with pointer but thet are not the same thing. Shotcut contains URL, but pointer contains memory address (at low level). And pointer can do much much much more, if you learn.
    slackware 10.0; kernel 2.6.7
    gcc 3.4.0; glibc 2.3.2; vim editor
    migrating to freebsd 5.4

  12. #12
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Alright, how much does an integer need ?

  13. #13
    still a n00b Jaguar's Avatar
    Join Date
    Jun 2002
    Posts
    187
    try sizeof (int_var), you will know it needs 4 bytes(32 bits).
    So it must range between -2^31 and 2^31-1.
    slackware 10.0; kernel 2.6.7
    gcc 3.4.0; glibc 2.3.2; vim editor
    migrating to freebsd 5.4

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM