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

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

P: n/a
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
Share this Question
Share on Google+
6 Replies


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

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

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

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

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.