pointers?

This is a discussion on pointers? within the C++ Programming forums, part of the General Programming Boards category; im starting to understand how pointers work but i dont understand the point. intead of using the address of a ...

  1. #1
    I'll take you down! polonyman's Avatar
    Join Date
    Sep 2004
    Posts
    50

    pointers?

    im starting to understand how pointers work but i dont understand the point. intead of using the address of a variable in an application like uni might be a pointer to the location of dog. so if you make sumthin like
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    
        int i = 10;
    
        do {
            cout << i << endl;
            i = i - 1;
        } while (i > 0);
    
        cin.get();
        
        return 0;
    }
    isnt it better to just use the variable i instead of the pointer?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What if you need to pass a huge amount of data some place so you can work with it? Say you've got 100MB of data you want to pass off to another function. Do you propose passing all of that by value?

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    You have 5 arrays of numbers, with a different number of elements in each array.
    Write a function which calculates the average of each array WITHOUT using any pointers.
    Then try it with a pointer
    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.

  4. #4
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    I think the preceeding wizards summed up why you'd really want to use pointers very nicely. Pointers enable you to reach out into main from a function and alter what is stored there.
    Just remember pointers don't have to be used all the time. Sometimes you do have a choice. C passes objects by value to preserve the integrity of your data, its actually a nice safeguard. When you must absolutely change an object - as quzah and salem expalined - you do so by going straight to its core - its address. As you get deeper into pointers you'll realise not only how powerful they are but also essential. You learn a great deal when you get there.
    Do a search for pointers on the board, you'll find a huge amount of documentation.
    Here is the classic case

  5. #5
    not-a-geek
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by quzah
    What if you need to pass a huge amount of data some place so you can work with it? Say you've got 100MB of data you want to pass off to another function. Do you propose passing all of that by value?
    Since these are the C++ boards, one should mention that this could be very well done with references - probably even should be done.

    As for the array problem, one would probably write a function to compute the average of one array and reuse it for all 5 arrays. Unless the whole averaging process is within another loop with a high number of iterations, the performance overhead is minimal and it allows that function to be reused for other purposes.
    Last edited by Nyda; 09-12-2004 at 09:15 AM.

  6. #6
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    try implementing a linked list without using pointers.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >try implementing a linked list without using pointers.

    Code:
    #include <iostream>
    #include <vector>
    
    struct node {
      int data;
      int next;
    
      node ( int init, int link )
        : data ( init ), next ( link )
      {}
    };
    
    int main()
    {
      std::vector<node> list;
    
      // Head insertion
      list.push_back ( node ( 0, -1 ) );
      for ( int i = 1; i < 10; i++ )
        list.push_back ( node ( i, i - 1 ) );
    
      for ( int i = list.size() - 1; list[i].next != -1; i = list[i].next )
        std::cout<< list[i].data <<"->";
      std::cout<<std::endl;
    
      list.clear();
    
      // Tail insertion
      list.push_back ( node ( 0, -1 ) );
      for ( int i = 1; i < 10; i++ ) {
        list.back().next = i;
        list.push_back ( node ( i, -1 ) );
      }
    
      for ( int i = 0; list[i].next != -1; i = list[i].next )
        std::cout<< list[i].data <<"->";
      std::cout<<std::endl;
    }
    My best code is written with the delete key.

  8. #8
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    that.. is mighty impressive i must say.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >that.. is mighty impressive i must say.
    A linked list is an abstraction, much like a stack or a queue. It consists of a sequence of nodes that have references to the next node in the sequence. To fully understand the abstraction you need to realize that the reference doesn't have to be a memory address as long as you can use it to find the node you want. From there it's obvious how to write a linked list without explicit pointers. It's also obvious how languages that lack pointers can implement linked data structures.
    My best code is written with the delete key.

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>try implementing a linked list without using pointers.
    Hm. How about passing a "N/A" value without pointers, when every possible value is valid?

    *EDIT: Ooops. Validation flags...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >*EDIT: Ooops. Validation flags...
    Yep. If all values are considered valid (or the error value is impractical) then you need another way of notifying the caller of an error. This is where things get interesting (and often ugly).
    My best code is written with the delete key.

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