By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,919 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

pointers (why should I learn them)

P: n/a
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

Jul 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
> 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


Jul 19 '05 #2

P: n/a
"Peter van Merkerk" <me*****@deadspam.com> wrote in message news:<bh************@ID-133164.news.uni-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
Jul 19 '05 #3

P: n/a
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[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.

Jul 19 '05 #4

P: n/a
On Mon, 11 Aug 2003 12:23:15 +0200, "Yame" <ya**********@hotmail.com>
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
Jul 19 '05 #5

P: n/a
"John Dibling" <dib@substitute_my_full_last_name_here.com> wrote in message
news:2k********************************@4ax.com...
On Mon, 11 Aug 2003 12:23:15 +0200, "Yame" <ya**********@hotmail.com>
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

Jul 19 '05 #6

P: n/a
On Tue, 12 Aug 2003 02:52:05 +0100, "Stuart Golodetz"
<sg*******@dial.pipex.com> 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
Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.