473,385 Members | 1,593 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,385 software developers and data experts.

What is the "point" of using pointers? (tutorial 4 retarded C programmers sufferrers of ADD also ;-) )

Ok. Let's say Im writing a game and I have a class table for my
character's stats. (Don't worry about classes yet ,you'll run into
them soon enough).

Say this table includes all his stats, age, strength, charisma, dex,
etc.. Say I got ten of these characters running around in my game, and
I want to write a function that if they go to the gym and work out
they get stronger.

Now I have my character John who I make an object of class characters.
So lets say john.strength is his strength attribute. Now I write a
function to make my character stronger.
Code:
void workout ()
{
john.strength = john.strength + 1;
}

Great, but what if I want to use that function for ALL my characters?
For Johny, Cindy, Barry and Mr. Rouchefort? If I couldnt use pointers
I'd have to do something ridiculous like
Code:
void workout (string name)
{
name.strength = name.strength +1;
}

And of course that wouldn't work, as it would assume I am calling a
character called "name" instead of putting the string there like I
want it. Ok so we have to use pointers..this makes it so much easier.
Code:

characters* ch; /* This creates a pointer ch that points to class
characters. Just go with me on this. */
ch = &john; /* The user wants to play as character john, so we set
this pointer equal to the address of john in memory */

void workout (characters *ch)
{
ch->strength = ch->strength+1;
}

Now we have a function that works for any of our ten characters. BTW -
is the same thing as using the dereference operator *, but the
syntax for object pointers is confusing so we use -instead to
dereference.

P.S.
Note: this is not my own tutorial; I googled it and made a few
changes.
I just found it to be a very down to Earth practical example for ppl.
to
understand the concept of pointers.
I hope it helps.

Nov 5 '07 #1
6 1565
=?utf-8?B?4piG4piG4piG4piG4piGIFPDvCBLZWl0aCBDaGFrb3RheS B2b24gQ2FycGF0aQ==?=
wrote:
>
Ok. Let's say Im writing a game and I have a class table for my
character's stats. (Don't worry about classes yet ,you'll run into
them soon enough).
Classes will be mentioned occassionally on this newsgroup
by people who don't know that there's a differnce between
the C and C++ programming languages.
Now I have my character John who I make an object of class characters.
"class character" doesn't mean anything in C.
characters* ch; /* This creates a pointer ch that points to class
characters. Just go with me on this. */
P.S.
Note: this is not my own tutorial; I googled it and made a few
changes.
I just found it to be a very down to Earth practical example for ppl.
to
understand the concept of pointers.
I hope it helps.
It was completely pointless on this newsgroup.

Try posting it to
news:comp.lang.c++

--
pete
Nov 5 '07 #2
pete said:
>
=?utf-8?B?4piG4piG4piG4piG4piGIFPDvCBLZWl0aCBDaGFrb3RheS B2b24gQ2FycGF0aQ==?=
wrote:
>>
<snip>
>I just found it to be a very down to Earth practical example for ppl.
to understand the concept of pointers. I hope it helps.

It was completely pointless on this newsgroup.

Try posting it to
news:comp.lang.c++
What have clc++ ever done to you, that you should hurt them so?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Nov 5 '07 #3
On Nov 5, 4:55 am, Sü Keith Chakotay von Carpati wrote:
Ok. Let's say I'm writing a game and I have a class table for my
character's stats. (Don't worry about classes yet ,you'll run into
them soon enough).
Comments on usage of 'class' already posted, no need to add more.
Say this table includes all his stats, age, strength, charisma, dex,
etc.. Say I got ten of these characters running around in my game, and
I want to write a function that if they go to the gym and work out
they get stronger.

Now I have my character John who I make an object of class characters.
So lets say john.strength is his strength attribute. Now I write a
function to make my character stronger.

Code:
void workout ()
{
john.strength = john.strength + 1;

}
This is C, so what you want is:
Code:
struct character { /* notice struct, not class */
int age, charisma, strength; /* Etc. */
} ;

void workout(struct character *mister)
{
mister->strength += 1; /* mister is a pointer, so use -*/
}

And you can have a loop, calling workout() for each character. Using
your example, the call would be:

struct character john, cindy, barry, MrRouchefort;
workout(&john);
Great, but what if I want to use that function for ALL my characters?
For Johny, Cindy, Barry and Mr. Rouchefort? If I couldnt use pointers
I'd have to do something ridiculous like
<snip>

See above. No more comments required, I hope.

-- Marty (a newbie, so feel free to ignore me) Wolfe

Nov 5 '07 #4
On Nov 5, 11:11 am, mazwo...@gmail.com wrote:
On Nov 5, 4:55 am, Sü Keith Chakotay von Carpati wrote:
Ok. Let's say I'm writing a game and I have a class table for my
character's stats. (Don't worry about classes yet ,you'll run into
them soon enough).

Comments on usage of 'class' already posted, no need to add more.
Say this table includes all his stats, age, strength, charisma, dex,
etc.. Say I got ten of these characters running around in my game, and
I want to write a function that if they go to the gym and work out
they get stronger.
Now I have my character John who I make an object of class characters.
So lets say john.strength is his strength attribute. Now I write a
function to make my character stronger.
Code:
void workout ()
{
john.strength = john.strength + 1;
}

This is C, so what you want is:
Code:
struct character { /* notice struct, not class */
int age, charisma, strength; /* Etc. */

} ;

void workout(struct character *mister)
{
mister->strength += 1; /* mister is a pointer, so use -*/

}

And you can have a loop, calling workout() for each character. Using
your example, the call would be:

struct character john, cindy, barry, MrRouchefort;
workout(&john);
Great, but what if I want to use that function for ALL my characters?
For Johny, Cindy, Barry and Mr. Rouchefort? If I couldnt use pointers
I'd have to do something ridiculous like

<snip>

See above. No more comments required, I hope.

-- Marty (a newbie, so feel free to ignore me) Wolfe
yeah, that's great, but the ideea was to simplify to the raw language.
you know, like for those folks that know only how to peel a banana.

Nov 6 '07 #5
Sü Keith Chakotay von Carpati wrote:
[snip]
yeah, that's great, but the ideea was to simplify to the raw language.
you know, like for those folks that know only how to peel a banana.
IMHO, the "raw language" doesn't need simplification. The point is that
programming (in any language) is hard, not because of the language, but
because making logical decisions and planning for the consequences (the
"practice of programming") is hard, in and of itself.

If you are one of those people who "know only how to peel a banana", then
making the language simpler won't help because you lack the knowledge,
expertise and discipline to actually write programs.

As for your "simplifications", they don't belong here. In comp.lang.c, we
discuss the tasks and techniques of writing programs in the C language, as it
is already defined. If you have a discussion of some proposed changes to the
definition of the language, you need to talk to the folks in comp.std.c
As for "classes", and all the rest of the OOification of C, good luck with
that. Hopefully, if you have success, it is well after I cease to design,
write, implement, and maintain programs written in C.

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------

Nov 9 '07 #6
On Nov 9, 1:49 pm, Lew Pitcher <lpitc...@teksavvy.comwrote:
Sü Keith Chakotay von Carpati wrote:
[snip]
yeah, that's great, but the ideea was to simplify to the raw language.
you know, like for those folks that know only how to peel a banana.

IMHO,
We all have the right to have opinions
I don't see why you make such a big deal of this topic.
the "raw language" doesn't need simplification.
That's what I've meant: "the ideea was to simplify TO the raw
language."
The point is that
programming (in any language) is hard, not because of the language, but
because making logical decisions and planning for the consequences (the
"practice of programming") is hard, in and of itself.
Thanks for sharing your thoughts on this one
>
If you are one of those people who "know only how to peel a banana", then
making the language simpler won't help because you lack the knowledge,
expertise and discipline to actually write programs.
Well, to me at least, the purpose of learning the language is be the
priority here,
regardless of the methods used into grasping the concepts.
What use will I or others have, to try to learn the language without
success
just because the only stuff available are some "incomplete manuals"
lacking the,
what I consider would be the most important thing required, that is
"practical
examples that I can grip using just a pair of claws"?

As for your "simplifications", they don't belong here. In comp.lang.c, we
discuss the tasks and techniques of writing programs in the C language, as it
is already defined.
Don't worry, I am not going to ruin comp.lang.c
There are off topics started by regulars here. At least I am on the
subject.

If you have a discussion of some proposed changes to the
definition of the language,
yeah, right ;-)
you need to talk to the folks in comp.std.c
tnx. for the tip; I've never new there is such a group before.

>
As for "classes", and all the rest of the OOification of C, good luck with
that. Hopefully, if you have success, it is well after I cease to design,
write, implement, and maintain programs written in C.

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------

Nov 10 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Will | last post by:
I don't know if I can explain what is happening but I will give it a try. I have 2 pages that display a set of thumbnail and they are almost identical. The only difference is the text file they...
2
by: Randy Crockett | last post by:
I have created a very simple DLL in VC++ 6.0, then created a very simple App in VC++ 6.0 to use the DLL and this works fine When I try to use the same DLL in C#, with DLLImport, the app finds the...
0
by: Lionel B | last post by:
Greetings, Using gcc (GCC) 3.3.3 (cygwin special) on Win2K Does anyone know if there is a convenient mechanism to cause a C++ exception to be thrown whenever a IEEE 754 floating-point...
3
by: jimfortune | last post by:
At what point is a Form added to the Forms collection or a Report added to the Report collection? I.e., listed as currently open. The reason I ask is that I have a subreport for an exclusive...
6
by: Martin Heuckeroth | last post by:
Hi, We are looking for a way to determine the x and y points of the cursor in a richtext box. We made an VB.NET application with a couple of listboxes and one of them is a richtextlistbox....
5
by: hzgt9b | last post by:
I'm building a dataset that writes out a Point type value. Here's the code that I've got: 1 Dim dsContent As DataSet = New DataSet("content") 2 Dim dtAsset As DataTable = New...
14
by: Alex | last post by:
I saw the topic of "wired code " about "point to array" and know a little about it. But I am still confused about the question below: I define a point to array "b" int (*b); then I locate the...
0
by: raylopez99 | last post by:
I ran afoul of this Compiler error CS1612 recently, when trying to modify a Point, which I had made have a property. It's pointless to do this (initially it will compile, but you'll run into...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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.