pointers (why should I learn them)

Discussion in 'C++' started by Yame, Aug 11, 2003.

  1. Yame

    Yame Guest

    Hi ^_^

    My name is Thomas Deschepper and I'm new to this newsgroup. For the moment
    I'm still running Windoze (please dont slap me :)

    I'm planning to install slack9 on the new PC that arrives in september.

    To keep me busy I'm learning C++.

    Here's my question: "Why are pointers so useful and why can't you just work
    with variables?"

    Bear in mind that I'm no guru ;) Just try to explain in simple English (I'm
    from Belgium, so it's not my native language)

    Thank you very mutch

    Thomas Deschepper
    Yame, Aug 11, 2003
    #1
    1. Advertising

  2. > Here's my question: "Why are pointers so useful and why can't you just
    work
    > with variables?"


    You need pointers for example to be able to build dynamic data
    structures. I cannot think of any non-trivial application that doesn't
    directly or indirectly uses pointers. So yes, you should learn about
    pointers; it not only required knowledge for C++ programmers, but the
    same concept is also used in many other programming languages.

    However the best place to find answers for your question is a good book.
    Usenet is not the ideal medium to explain general topics like pointers.
    For book reviews see : www.accu.org.

    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
    Peter van Merkerk, Aug 11, 2003
    #2
    1. Advertising

  3. Yame wrote:

    > Hi ^_^
    >
    > My name is Thomas Deschepper and I'm new to this newsgroup. For the moment
    > I'm still running Windoze (please dont slap me :)
    >
    > I'm planning to install slack9 on the new PC that arrives in september.



    This is not a Linux group. We don't care what OS you are running.

    >
    > To keep me busy I'm learning C++.
    >
    > Here's my question: "Why are pointers so useful and why can't you just work
    > with variables?"


    Pointers are how C++ does things. You can do very little without them.
    The most common use us to refer to dynamic memory - in other words, an
    object created at runtime that has no name.

    int *p = new int;

    Now p is a pointer to an int. There is no way to refer to that int other
    than using a pointer to it.

    Pointers are also used to get at elements of an array:

    int array[100];
    for (int *p = array; p != array + 100; ++p)
    {
    // do something with *p
    }

    Of course I could also do this:

    int array[100];
    for (int i = 0; i != 100; ++i)
    {
    // do something with array
    }

    But array indexing also uses pointers, whether it looks like it or not.
    This:

    array

    is exactly equivalent to this:

    *(array + i)

    Where array becomes, for all intents and purposes, a pointer to the
    first element of the array.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
    Kevin Goodsell, Aug 11, 2003
    #3
  4. Yame

    Bill Hanna Guest

    "Peter van Merkerk" <> wrote in message news:<bh7vm1$vkjhg$-berlin.de>...
    > > Here's my question: "Why are pointers so useful and why can't you just

    > work
    > > with variables?"

    >
    > You need pointers for example to be able to build dynamic data
    > structures. I cannot think of any non-trivial application that doesn't
    > directly or indirectly uses pointers. So yes, you should learn about
    > pointers; it not only required knowledge for C++ programmers, but the
    > same concept is also used in many other programming languages.
    >
    > However the best place to find answers for your question is a good book.
    > Usenet is not the ideal medium to explain general topics like pointers.
    > For book reviews see : www.accu.org.



    Pointers are required to address memory-mapped I/O and to index
    variables in an array.

    Bill Hanna
    Bill Hanna, Aug 11, 2003
    #4
  5. Yame

    John Dibling Guest

    On Mon, 11 Aug 2003 12:23:15 +0200, "Yame" <>
    wrote:


    >Here's my question: "Why are pointers so useful and why can't you just work
    >with variables?"


    Object-Oriented programming (eg, polymorphism) is not possible without
    pointers. For instance, the following code defines an abstract
    interface class (Dog) with one method (Bark). It also defines 2
    specific types of dogs (Eskimo and Sheppard) that bark in different
    ways. By instantiating an Eskimo and calling the Bark() method
    through the Dog (base class) pointer, you are using polymorphism; a
    cornerstone of object-oriented programming. If is not possible to
    accomplish this behavior without using pointers (or references which
    can be thought of as dereferenced pointers).

    Here's some sample code, in 1 file which you can compile & run as-is:

    #include <cstdlib>
    #include <iostream>
    #include <vector>
    #include <algorithm>

    using namespace std;

    class Dog
    {
    public:
    virtual void Bark() const = 0;
    };

    class Eskimo : public Dog
    {
    public:
    void Bark() const { cout << "Yip! Yip!" << endl; }
    };

    class Sheppard : public Dog
    {
    public:
    void Bark() const { cout << "BOW WOW!!! BOW WOW!!!" << endl; }
    };


    int main()
    {
    // prologue
    typedef vector<Dog*> Dogs;
    Dogs vDogs;
    Dogs::iterator itDog; // this is in function-scope to get around
    a stupid MSVC bug; should be fine with all compilers
    // build a vector of different types of dogs
    vDogs.push_back(new Eskimo);
    vDogs.push_back(new Sheppard);
    // make all the dogs bark
    for( itDog = vDogs.begin(); vDogs.end() != itDog; ++itDog )
    {
    // get the dog
    Dog* pDog = *itDog;
    // bark the dog
    pDog->Bark();
    }
    // deallocate resources
    for( itDog = vDogs.begin(); vDogs.end() != itDog; ++itDog )
    {
    // get the dog
    Dog* pDog = *itDog;
    // delete the dog object
    delete pDog;
    }
    vDogs.clear();
    // epilogue
    return 0;
    }


    </dib>
    John Dibling
    Witty banter omitted for your protection
    John Dibling, Aug 11, 2003
    #5
  6. "John Dibling" <dib@substitute_my_full_last_name_here.com> wrote in message
    news:...
    > On Mon, 11 Aug 2003 12:23:15 +0200, "Yame" <>
    > wrote:

    <snip>
    > Dogs::iterator itDog; // this is in function-scope to get around
    > a stupid MSVC bug; should be fine with all compilers


    Here's a workaround for VC6's scoping problem:

    #define for if(0); else for

    HTH,

    Stuart.

    <snip>
    > </dib>
    > John Dibling
    > Witty banter omitted for your protection
    Stuart Golodetz, Aug 12, 2003
    #6
  7. Yame

    John Dibling Guest

    On Tue, 12 Aug 2003 02:52:05 +0100, "Stuart Golodetz"
    <> wrote:

    >Here's a workaround for VC6's scoping problem:


    Hey thanks, a bonus! Usually I do something like this in real code,
    but I'll look at your idea...

    { // open scope
    for( Dogs::iterator itDog = vDogs.begin(); vDogs.end(0 != itDog;
    ++itDog )
    {
    /* ... */
    } // for()
    } // close scope

    takes care of the scoping problem in a way the standard says for(...)
    should work.


    </John Dibling>
    Witty banter omitted for your protection
    John Dibling, Aug 12, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anonieko

    HttpHandlers - Learn Them. Use Them.

    Anonieko, Jun 15, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    507
    tdavisjr
    Jun 16, 2006
  2. Porky Pig Jr
    Replies:
    3
    Views:
    1,018
    Fuzzyman
    May 12, 2004
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,765
    Smokey Grindel
    Dec 2, 2006
  4. windandwaves

    why should I learn python

    windandwaves, Sep 6, 2007, in forum: Python
    Replies:
    34
    Views:
    757
    HowiPepper
    Sep 11, 2007
  5. why the lucky stiff
    Replies:
    5
    Views:
    136
    why the lucky stiff
    Sep 22, 2004
Loading...

Share This Page