pointers (why should I learn them)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Yame

    pointers (why should I learn them)

    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





  • Peter van Merkerk

    #2
    Re: pointers (why should I learn them)

    > Here's my question: "Why are pointers so useful and why can't you just
    work[color=blue]
    > with variables?"[/color]

    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.merke rk(at)dse.nl






    Comment

    • Bill Hanna

      #3
      Re: pointers (why should I learn them)

      "Peter van Merkerk" <merkerk@deadsp am.com> wrote in message news:<bh7vm1$vk jhg$1@ID-133164.news.uni-berlin.de>...[color=blue][color=green]
      > > Here's my question: "Why are pointers so useful and why can't you just[/color]
      > work[color=green]
      > > with variables?"[/color]
      >
      > 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.[/color]


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

      Bill Hanna

      Comment

      • Kevin Goodsell

        #4
        Re: pointers (why should I learn them)

        Yame wrote:
        [color=blue]
        > 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.[/color]


        This is not a Linux group. We don't care what OS you are running.
        [color=blue]
        >
        > 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?"[/color]

        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[i]
        }

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

        array[i]

        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.

        Comment

        • John Dibling

          #5
          Re: pointers (why should I learn them)

          On Mon, 11 Aug 2003 12:23:15 +0200, "Yame" <yame_conoces@h otmail.com>
          wrote:

          [color=blue]
          >Here's my question: "Why are pointers so useful and why can't you just work
          >with variables?"[/color]

          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

          Comment

          • Stuart Golodetz

            #6
            Re: pointers (why should I learn them)

            "John Dibling" <dib@substitute _my_full_last_n ame_here.com> wrote in message
            news:2k5gjv0qfd buqpnn54ot11pd9 kbvamqrmi@4ax.c om...[color=blue]
            > On Mon, 11 Aug 2003 12:23:15 +0200, "Yame" <yame_conoces@h otmail.com>
            > wrote:[/color]
            <snip>[color=blue]
            > Dogs::iterator itDog; // this is in function-scope to get around
            > a stupid MSVC bug; should be fine with all compilers[/color]

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

            #define for if(0); else for

            HTH,

            Stuart.

            <snip>[color=blue]
            > </dib>
            > John Dibling
            > Witty banter omitted for your protection[/color]


            Comment

            • John Dibling

              #7
              Re: pointers (why should I learn them)

              On Tue, 12 Aug 2003 02:52:05 +0100, "Stuart Golodetz"
              <sgolodetz@dial .pipex.com> wrote:
              [color=blue]
              >Here's a workaround for VC6's scoping problem:[/color]

              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

              Comment

              Working...