473,756 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1582
=?utf-8?B?4piG4piG4pi G4piG4piGIFPDvC BLZWl0aCBDaGFrb 3RheSB2b24gQ2Fy cGF0aQ==?=
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?4piG4piG4pi G4piG4piGIFPDvC BLZWl0aCBDaGFrb 3RheSB2b24gQ2Fy cGF0aQ==?=
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 "simplification s", 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...@teksa vvy.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 "simplification s", 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.digital freehold.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
1488
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 point to that holds the picture information. I have a php script that reads the text file and lays out the thumbnails and creates a link to a page that shows the full image. The first thing the html file does is open a session so I can store...
2
537
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 DLL just fine but then says "Unable to find an entry point When I try to use "user32.dll" everything works, but doesn't with my DLL I am using .NET Framework 1.1 and VisualStudio .NET 2003 on Windows 2000 Pro
0
1587
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 "exception" is raised? Currently I a check the FPU status word wherever I wish to test for FP errors and throw a C++ exception accordingly. This is fine; but
3
2405
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 separate condition that may or may not have data. When it does have data its total needs to be added to the final report total. I used a function like: Public Function SubreportHasData(strReport As String, strSubReport As String) As Boolean Dim...
6
4643
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. After a refresh the cursor of the richtextlistbox is reset and goes to top. And that's not a cool thing when you are reading at the bottom of that box and have to look up the point where your were reading manual to have it reset after another
5
4367
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 DataTable("asset") 3 Dim dcPk(0) As DataColumn 4 dcPk(0) = _ 5 dtAsset.Columns.Add("fullName", Type.GetType("System.String")) 6 dtAsset.Columns.Add("videoLocation", Type.GetType("System.Drawing.Point"))
14
2262
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 memory to b,using "malloc" b= (???) malloc( 10*sizeof ( *b ) ) Here,if I want to convert the type by force, which shall I use? I use: b=(int**)malloc(10*sizeof(*b)); the code can compile, but there is a warring "assignment from
0
2195
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 problems later). Apparently Point is a struct, a value type, and it does not behave like a classic structure (in my mind's eye, and see below). Traditionally I think of a classic structure as simply an object where every member is public. But with...
0
9462
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9287
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9857
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9722
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7259
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3817
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3369
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2677
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.