Pointer Clarification

Started by
14 comments, last by Lactose 8 years, 8 months ago

Hmm, well while I attempt what you asked, I am realizing that building an Array to represent items like HP values and Names isn't working very well... So, is it safe to say that the following is possible:

Goblin 1's Address (which is Pointed to...): 123

I could pull specific information from that address such as HP values and names? I ask because I was under the impression only a specific type of integer could be stored at the address, i.e. and INT or STRING value, for example.

I realize that I might only confuse you more at this point. I'll continue on KnonalCross's analogy as that seems to have resonated well with you.


This is an amazing analogy! Thank you! Similar to a question I asked above. Would the 'coat' hold multiple variables? (i.e. Name of the coat, color, size, etc.?)

The coat could hold all kinds of stuff in it's pockets, it could even hold another paper with 'Wardrobe 17' on it. That would mean that for the person who has their belongings(could be a coat, scarf etc.) in wardrobe 17, would first have to get the coat from 16, in order to pull out the paper and get their belongings from 17.

Advertisement
Tldr

Not on my computer, so forgive me. This is how pointers work.

// code

void Goblin::Attack(int targetHP)
{
targetHP -= 5;
}

Goblin oEnemy;
int playerHP = 100;

oEnemy.Attack(playerHP);

/**
Now, what this actually does is copy the value of playerHP (100) to targetHP, reduce it by 5 (95), then promptly throw it away because the function is done and targetHP no longer exists. playerHP is unchanged.

What you want to do is modify the original variable, not copy it. So you could use a pointer.
*/

void Goblin::Attack(int *targetHP)
{
targetHP-= 5;
}

oEnemy.Attack(&playerHP);

This time, the original variable is modified. It is now 95.

PS. You can use pointers for various useful tasks. I recommend googling polymorphism and array pointer logic.
Reusing previous answer.

Here are common reasons why people use pointers:

0) A particular object or array is large, and they don't want to incur a copy of it when passing to a function. Passing a pointer is small (typically four or eight bytes), versus copying potentially thousands or millions of elements in an array.

Note: C++ provides references for this.

1) The programmer wishes to add an optional parameter to a function. If the parameter is not important, the programmer will pass a nullptr. Otherwise, they pass the address of an interesting object. The function can test if the pointer is null before using it.

2) The programmer wishes an object to live beyond the scope it is created in. Typical C++ objects live in a given scope, and can only be copied outside that scope. By dynamically allocating the object and storing a pointer to it, the original object can live longer than its scope and can be deleted later. An example in a game is a bullet, there might be a shoot() function, but the bullet should live until it hits something, not be destroyed at the end of the function.

Note: modern C++ recommends using smart pointers for allocating and managing the ownership of such objects.

3) The programmer has a rich ownership convention, where raw pointers convey the concept of "this object does not own the other object". Care must be taken with this approach to avoid dangling pointers. A possible alternative is a "weak pointer". An example in a game might be an AI object's current target.

4) Interacting with C style APIs. Some APIs pre-date C++, or otherwise are written in a rather simple fashion (e.g. for crossing DLL boundaries). In this case you might be forced to deal with these pointers. An example in a game might be the SDL library, which returns the "screen" surface as a raw pointer from SDL_SetVideoMode().

Note: Modern C++ style recommends "wrapping" the code that deals with such objects to some degree, at the very least using smart pointers to manage their lifecycle.


void Goblin::Attack(int *targetHP)
{
targetHP-= 5;
}

Just pointing (har-har-har) out that this wouldn't work correctly.

Since targetHP is a pointer, you'd need to dereference it before modifying it.

*targetHP -= 5;

Hello to all my stalkers.


void Goblin::Attack(int *targetHP)
{
targetHP-= 5;
}

Just pointing (har-har-har) out that this wouldn't work correctly.

Since targetHP is a pointer, you'd need to dereference it before modifying it.

*targetHP -= 5;

Autocorrect? I spent 20 minutes typing that post on my cell phone whilst it changed every single word/character that I typed. Not in the mood for snarky behavior.


Not on my computer, so forgive me.

Yeah.


Autocorrect? I spent 20 minutes typing that post on my cell phone whilst it changed every single word/character that I typed. Not in the mood for snarky behavior.

No snark intended.

I guessed phone typing was the reason, but I thought a minor correction might be worth it for beginners reading and trying to understand your post :)

The "har-har-har" was because of the (horrible) pointer-related pun, not related to your post.

Hello to all my stalkers.

This topic is closed to new replies.

Advertisement