473,378 Members | 1,317 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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

Jul 19 '05 #1
6 1709
> 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
"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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

53
by: Alf P. Steinbach | last post by:
So, I got the itch to write something more... I apologize for not doing more on the attempted "Correct C++ Tutorial" earlier, but there were reasons. This is an UNFINISHED and RAW document,...
388
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's...
3
by: user | last post by:
Hi all, At the outset, I regret having to post this slightly OT post here. However, I strongly feel that people in this group would be the best to advise me on my predicament. I am working as...
19
by: s.subbarayan | last post by:
Dear all, I had this following doubt,while java is able to carryon with out pointers why C language cant be modified to remove pointer?Hows java able to do this with out pointers? I jus plead...
12
by: Lance | last post by:
VB.NET (v2003) does not support pointers, right? Assuming that this is true, are there any plans to support pointers in the future? Forgive my ignorance, but if C# supports pointers and C# and...
59
by: MotoK | last post by:
Hi Experts, I've just joined this group and want to know something: Is there something similar to smart pointers in C or something to prevent memory leakages in C programs. Regards MotoK
92
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
10
by: arnuld | last post by:
thanks
69
by: Yee.Chuang | last post by:
When I began to learn C, My teacher told me that pointer is the most difficult part of C, it makes me afraid of it. After finishing C program class, I found that all the code I wrote in C contains...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.