Pointers.

This is a discussion on Pointers. within the C++ Programming forums, part of the General Programming Boards category; Could somebody explain pointers to me? I've read the section on pointers twice now, but I still don't have a ...

  1. #1
    Dumb to the power of 999. Wall's Avatar
    Join Date
    Aug 2004
    Posts
    13

    Exclamation Pointers.

    Could somebody explain pointers to me? I've read the section on pointers twice now, but I still don't have a clue what they're for and what they do. I feel like I'm bugging you all with this n00b problem but I'm pretty determined to having adequate knowledge of C++.

    - Wall
    My head hurts.

  2. #2
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    google is your best friend.

  3. #3
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    A good analogy is your home address. Your address is where you live. You are the contents stored at that address. A pointer holds the address of a variable,structure,object, function, array etc . A pointer doesn't hold the actual data just the address of the data. This is extremely useful when you want to pass large blocks of data back and forth. Instead of copying the data you can just get a pointer to it.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  4. #4
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by manofsteel972
    A good analogy is your home address. Your address is where you live. You are the contents stored at that address. A pointer holds the address of a variable,structure,object, function, array etc . A pointer doesn't hold the actual data just the address of the data. This is extremely useful when you want to pass large blocks of data back and forth. Instead of copying the data you can just get a pointer to it.
    I was actually going to break it down for the op but for most ppl, pointers are a bit more indepth and they tend to confuse it easily. I would do a search on the board OP, and i'd also use google.

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Just think of a pointer as a laser pointing at an option.

    Kuphryn

  6. #6
    Dumb to the power of 999. Wall's Avatar
    Join Date
    Aug 2004
    Posts
    13
    I think I kind of understand it now. Could somebody give me an example of some code that uses pointers? So I understand the point of pointers. (heh)
    My head hurts.

  7. #7
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by Wall
    I think I kind of understand it now. Could somebody give me an example of some code that uses pointers? So I understand the point of pointers. (heh)

    dude your not serious??. I just gave you a link to a whole bunch of tutorials and code. did you even click the link?? and no you don't understand it already, even adepts get stumped with pointers still. MEH. rtfPOST!

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    113
    here you have some samples:


    Code:
    #include <iostream>
    using namespace std;
    
    //to test pointer
    void testPtr(int *ptrLocal)
    {
        *ptrLocal = 300;
    }
    
    //to test without pointer
    void testByVal(int intLocal)
    {
        intLocal = 300;
    }
    
    int main()
    {
        //integer containign value 100
        int myInt = 100;  
      	
      	//pointer to myInt(anything you do with ptrInt affects myInt too
      	//because you can see it like if its an allias of myInt
      	int *ptrInt = &myInt;
      	
      	//displays myInt original value
        cout << "Original value of myInt: " << myInt << endl;
      	
        //value(200) to ptrInt and affects myInt too
      	*ptrInt = 200;
      	cout << "Value of myInt after \"*ptrInt = 200\": " << myInt << endl;
        
        //passing myInt to test() by value(copy of myInt)
        //will not modify myInt        
        testByVal(myInt);
        cout << "Value of myInt after testByVal(myInt): " << myInt << endl;
        
        //passing direction(pointer) of myInt to test()
        //will modify myInt
        testPtr(&myInt);
        cout << "Value of myInt after testPtr(&myInt): " << myInt << endl;
        
        return 0;
    }
    you can use pointers for a lot of things, but 1 thing you must keep in mind is that you can perform some tasks without the use of pointers but working that way you give up performance, why?
    ie: when u pass data to a function(like "testPtr" or "testByValue") the compiler creates a copy of that data to work with it, know imagine you are passing a big value like a big array of doubles, big objects etc, the compiler will create a copy of that and all this means less memory, but if you send a pointer to a funciton the compiler will not create a copy because you are sending the memory direction of the data in other words you are working directly to the data.

    another use could be to navigate data like an array, you can use a pointer to that array to navigate the array like finding a special char modify a value(element) of that array, etc you can do that because you can point that pointer to any place(see pointer aritmethic )

    hope it helps and plase excuse my poor english

    if you dont understand me just forget it
    Last edited by terracota; 08-19-2004 at 11:39 AM.

  9. #9
    Loom Weaver the dead tree's Avatar
    Join Date
    Apr 2004
    Posts
    140

  10. #10
    Dumb to the power of 999. Wall's Avatar
    Join Date
    Aug 2004
    Posts
    13
    dude your not serious??. I just gave you a link to a whole bunch of tutorials and code. did you even click the link?? and no you don't understand it already, even adepts get stumped with pointers still. MEH. rtfPOST!
    o.o;;;;

    I just wanted to see some blocks of code where pointers would come in handy
    My head hurts.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    This one is pretty good for the regular kinds of pointers
    http://pweb.netcom.com/~tjensen/ptr/pointers.htm

    This one for the scary stuff of when you want to point to functions
    http://www.function-pointer.org/
    Actually, they're pretty easy once you get past the syntax (and then hide it behind typedefs)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    The point of pointers...

    So I understand the point of pointers.
    The most common use of pointers:
    A function can only return one value. If your function needs to modify more than one variable, you need to pass-in the addresses of those values. The classic first-example is where you want to swap the values of two variables. (If you're working from a book, it will most likely have a swap-example.)

    You'll use pointers (or references) when your function deals with arrays, structures, or objects.

  13. #13
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    I just wanted to see some blocks of code where pointers would come in handy
    well here is an option then.
    PHP Code:
    /*finds yearly totals, yearly average, and monthly average for several years of rainfall data using ptr notation */
    #include <stdio.h>
    #define MONTHS 12    
    #define YEARS   5    
    int main(void)
    {
     
    // initializing rainfall data 
     
    const float rain[YEARS][MONTHS] = {
     {
    10.18.16.84.22.11.80.20.31.12.36.17.4},
     {
    9.29.84.43.32.20.80.40.00.61.74.35.2},
     {
    6.65.53.82.81.80.20.00.00.01.32.64.2},
     {
    4.34.34.33.02.01.20.20.20.42.43.56.6},
     {
    8.58.21.21.62.40.05.20.90.30.91.47.3}
     };
     
    int yearmonth;
     
    float subtottotal;
     const 
    float (*data)[MONTHS];
     
    data rain;

     
    printf(" YEAR    RAINFALL  (inches)\n");
     for (
    year 0total 0year YEARSyear++)
     {             
    // for each year, sum rainfall for each month
        
    for (month 0subtot 0month MONTHSmonth++)
           
    subtot +=  *(*(rain year) + month); 
           
    /* or data[year][month] */
        
    printf("%5d %15.1f\n"1997 yearsubtot);
        
    total += subtot;                  // total for all years 
     
    }
     
    printf("\nThe yearly average is %.1f inches.\n\n"total/YEARS);
     
    printf("MONTHLY AVERAGES:\n\n");
     
    printf(" Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct ");
     
    printf(" Nov  Dec\n");

     for (
    month 0month MONTHSmonth++)
     {             
    // for each month, sum rainfall over years
        
    for (year 0subtot =0year YEARSyear++)
           
    subtot +=  *(*(rain year) + month);
        
    printf("%4.1f "subtot/YEARS);
     }
     
    printf("\n");
     
    getchar();
     return 
    0;

    note: this could easily have been written in array notation as well. As you'll see sometimes it is not mandatory to use pointers. "the dual nature of them tends to confuse ppl"

    salem's two links are pretty good as well.
    Last edited by caroundw5h; 08-19-2004 at 01:12 PM.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

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, 09:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21