Jump to content
  • Advertisement
Sign in to follow this  
Gor435

Very Confused

This topic is 4971 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok here goes. I have little C++ programming experence but i am working on it i have multiple books i have read and some i am still in the process of reading i have read multiple tutorials and i feel i have a good understanding of most of the C++ language. Some things still elude me like pointers (i know what they are i know how they work i know how to use them but i am confused as to the purpose of them). Anyway my point is im confused i have tried and tried to make sense of certain things involving Windows API, OpenGl, And DirectX. In other words i can make black and white text on the screen console style and i can make a window and I really need MORE :=( Can anyone assist me???

Share this post


Link to post
Share on other sites
Advertisement
1) not many people can say they know most of the c+ language. You think you know it all, and then bam there is something new to learn about.

2)For opengl try nehe tutorials, they are great nehe.gamedev.net They are on the left.

Share this post


Link to post
Share on other sites
Pointers are confusing at first for most people. The topic comes up here with some frequency, so try searching on the topic to review past question and answer sessions about them. Also look through the articles.

In short, a pointer is a variable that "points" to a memory location. That memory location holds a value of some kind - an integer, a floating point number or a series of character codes (typically ANSI codes - ie. a string). A pointer also occupies memory, which makes the existence of "pointers to pointers" possible. Pointers can be nested to deeper levels, but the deepest you'll probably ever need is pointer to pointer to pointer and then only on very rare occaisions. Pointers and pointer to pointers are far more commonly used.

Pointers can be used for many purposes. Quite frequently they are used to index memory locations in a block of memory. For example, scanning a string for a particular character.

If you have a question about a particular bit of code, share it with your question and I'm sure people will step up to help.

Share this post


Link to post
Share on other sites



The first time i learnt about pointers i was confused too.

Then one day i tried to make Hangman and i think it was possible WITHOUT pointers though very confusing. Then i thought about using pointers and everything got so simple. (funny how something that's confusing can make other things simpler)
Anyway, if you dont understand the way its explained (or rather its use) in one book/tutorial i would really advise reading a different tutorial/ resource to complement what you already know. And dont rush it,..it'll hit you eventually how you could think they werent useful.
And it sounds like you're learning C++, openGL, win32 API, etc all at once.
I'd advice doing c++ first, then WIN32, then openGL/DirectX. (openGL for me)
Oh yeah,..and i in no way intended to imply that pointers are only useful for coding hangman :D

Share this post


Link to post
Share on other sites
Thank you guys for the info i guess my main problem is trying to learn too much at once but i just want to learn this stuff im good a taking it slow but i seem to have a problem when it comes to learning something new :( I JUST WANNA KNOW IT ALL :)) lol

Share this post


Link to post
Share on other sites
Original post by Gor435
Some things still elude me like pointers (i know what they are i know how they work i know how to use them but i am confused as to the purpose of them). quote]

Their are several reasons why we use pointers lets look at a few. (Note simple examples and probably not the best of example)


struct Player
{
int life;
int amunition;
int speed;
RECT boundingBox;
};



Now lets say we have a function called harm player.


void HarmPlayer(Player playerToHarm, int amount)
{
playerToHarm.life -= amount;
}



what's happening here? Well first you need to know a bit about passing arguments to a function. You will recall in your studies that the above function is passing Player by value, i.e. we create a copy of player when we pass it in.


Player ourPlayer; //assume life starts at 100
HarmPlayer(ourPlayer, 10);

//ourPlayers life still equals 100, why? because we created a copy of player
//when we passed ourPlayer to the function.



now lets look at

void HarmPlayer(Player * pPlayerToHarm, int amount)
{
pPlayerToHarm->life -= amount;
}



this is called passing by refrence. We use a p infront of playerToHarm to denote it's a pointer, this is commonly used.

What happens here is instead of creating a player we just pass the address of the block of memory that holds the player... i.e. And when we derefrence the block of memory we can change the actual value at that memory... sooo



Player ourPlayer; //assume life starts at 100
HarmPlayer(&ourPlayer, 10);

//ourPlayers life now equals 90, why? because we passed the "actual" player memory and not a copy



in c++ we can pass by refrence in another way, i.e. we don't want to create a copy of the object... this is done in the function definition. Notice the & sign.



void HarmPlayer(Player & playerToHarm, int amount)
{
playerToHarm.life -= amount;
}




Another reason we pass pointers, is for performance. If we pass by value we creae a copy of an object which invokes a constructer and may do some other stuff, then a destructor is called yadda yadda yadda... (Sometimes we may want this behavior) Also you will note passing something by value passes sizeof(theObject) which could be very large, while a pointer is only 4 bytes very very tiny, so yeah maybe that made sense?

Share this post


Link to post
Share on other sites
pointers can also simplify huge blocks of code

if you have a vector in the following structures, and you're indexing through constants (just pretend) it could end up as fugly as this:

teams[TEAM_BLUE].players[TEAM_PLAYER_CAPTAIN].position.x = 0;
teams[TEAM_BLUE].players[TEAM_PLAYER_CAPTAIN].position.y = 0;
teams[TEAM_BLUE].players[TEAM_PLAYER_CAPTAIN].position.z = 0;

yuck...
now you can just do this instead:

Vector* p;
p = &team[TEAM_BLUE].players[TEAM_PLAYER_CAPTAIN].position;
p->x = 0;
p->y = 0;
p->z = 0;

and in loops it makes everything easier to understand... until you no longer even have to think about it anymore.

Share this post


Link to post
Share on other sites
Quote:
Original post by Lee_
pointers can also simplify huge blocks of code

if you have a vector in the following structures, and you're indexing through constants (just pretend) it could end up as fugly as this:

teams[TEAM_BLUE].players[TEAM_PLAYER_CAPTAIN].position.x = 0;
teams[TEAM_BLUE].players[TEAM_PLAYER_CAPTAIN].position.y = 0;
teams[TEAM_BLUE].players[TEAM_PLAYER_CAPTAIN].position.z = 0;

yuck...
now you can just do this instead:

Vector* p;
p = &team[TEAM_BLUE].players[TEAM_PLAYER_CAPTAIN].position;
p->x = 0;
p->y = 0;
p->z = 0;

and in loops it makes everything easier to understand... until you no longer even have to think about it anymore.


this is one of the things that references are for.


Vector &p = team[TEAM_BLUE].players[TEAM_PLAYER_CAPTAIN].position;
p.x = 0;
p.y = 0;
p.z = 0;

Share this post


Link to post
Share on other sites
Ahem. Put responsibility where it belongs, please.
team[TEAM_BLUE].players[TEAM_PLAYER_CAPTAIN].setPosition(0,0,0);

(Or even:
game.setPlayerPosition(TEAM_BLUE, TEAM_PLAYER_CAPTAIN, 0, 0, 0);
with the appropriate delegation happening down the line.)

But I digress.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!