• Advertisement
Sign in to follow this  

[FIXED!] Using double pointers (**pointer) classes, and functions

This topic is 4279 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

wohoo! fixed ^_^. Thanks again for the help, seems I just made some relaly dumb tired errors o_oU. [Edited by - Dhaos on May 7, 2006 8:59:10 PM]

Share this post


Link to post
Share on other sites
Advertisement
To get it to build you need to change .multiplier[0]=&set_to_two; remove the & sign from in front of set_to_two. multiplier is an array of integers and you are trying to set the value in postion 0 with a pointer to an integer. Then repeat that for the two other times this is done in this function.

Share this post


Link to post
Share on other sites
While that does allow it to compile, the program crashes once it gets to the calculate damage function, I don't know why, probably has something to do with my limited knowledge of pointers.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
My advice would be to start with something simpler. From looking at your program it seems evident that you lack understanding of simple pointer concept. For example:

int *stat_a;//holds stat to multiply * is bad...?
int *stat_b;
int *stat_c;

Even in a test environment for pointers, this is a bad idea. You want to understand pointers, that's good. But use them in the proper context. If you want an stat that is an int, make it an Int...not a pointer to an Int. Plus, you need to learn that if you have a class that contains data like pointers, that you need a proper copy contructor and an equal operator. You also need to initialize those pointers values and have a destructor that frees up the memory.

(*character).c_strength=20;

a simple character->c_strength would have worked just as well. You might also want to check for passing parameter by reference.

Again, make your test simpler. Create smaller step so that you can assert yourself every step of the way. When you get at a point where you are blocked, backtrack a step and make sure you understand what's not working before moving onwards.

Check out the following to start: http://www.cplusplus.com/doc/tutorial/pointers.html and checkout other tutorial you can find online.

Good luck!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Your program is most likely crashing because you never are initializing the stat_a.

damage = Calculate_Damage( *c_player[0].c_skill[0].stat_a,c_player[0].c_skill[0].multiplier[0],
*c_player[0].c_skill[0].stat_b,c_player[0].c_skill[0].multiplier[0]) - c_enemy[0].c_endurance;

You are dereferencing invalid pointers. Don't make stat_a,b,c pointers.

Share this post


Link to post
Share on other sites
Hmm I see your point I suppose. Ironically that was the site I was studying. Something about pointers is not registering. And I am lost even after reading and studying extensively. I did start off with a much much simpler app that just fiddle with pointers (went like this...and it worked perfectly)

original program:

#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#include <time.h>

int global_one=1;

int initialize=0;

int Calculate_Damage(int stat_1,int multiplier_1,
int stat_2,int multiplier_2)
{
int damage=0;
damage = stat_1*multiplier_1;
return damage;
}

class C_Character;
class C_Skill;

class C_Skill
{
public:
char name[64];
int damage_physical;
int damage_fire;
int *stat_a;//holds stat to multiply * is bad...?
int *stat_b;
int *stat_c;
int multiplier[2];//holds value to multiply stat with
};

class C_Character
{
public:
int chp;
int mhp;
int c_strength;
int c_spirit;
int c_endurance;
C_Skill c_skill[8];
};
C_Character c_player[5],c_enemy[5];

class C_Sub_Global
{
public:
int *p[4];
};

class C_Global
{
public:
int a;
int b;
int c;
C_Sub_Global c_sub_global;

}; C_Global c_global;


int Test(int x, int y, int z,int moo)
{
int value=x*y;
return value;
}

void Set_Skill(C_Character **character,int id)
{
int set_to_two=0;
////////setup claw
strcpy((**character).c_skill[0].name,"CLAW");

(**character).c_skill[0].stat_a=&(**character).c_strength;
(**character).c_skill[0].multiplier[0]=set_to_two;
////////set mult1
(**character).c_skill[0].stat_b=&global_one;//no *clas_stuff.bleh.moo=#
(**character).c_skill[0].multiplier[1]=global_one;
////////set mult2
(**character).c_skill[0].stat_c=&global_one;
(**character).c_skill[0].multiplier[2]=global_one;
}

void Initialize(C_Character *character,int id)
{
if(id==0)//player
{
////////player
(*character).c_strength=20;
(*character).c_endurance=15;
(*character).c_spirit=15;
(*character).chp=(*character).c_endurance*3;
(*character).mhp=(*character).c_endurance*3;

}
if(id==1)//enemy
{
////////player
(*character).c_strength=15;
(*character).c_endurance=12;
(*character).c_spirit=11;
(*character).chp=(*character).c_endurance*3;
(*character).mhp=(*character).c_endurance*3;
}
}

void main()
{
c_global.a=10;
c_global.b=3;
c_global.c_sub_global.p[0]=&c_global.a;//warks

c_global.c=Test(*c_global.c_sub_global.p[0],c_global.b,0,0);

cout << c_global.c << endl;

int var_a=10;
int var_b=3;

int *var_c=NULL;

var_c = &var_a;//assign memory address of var_a to var c

int var_d=0;
//var_d=*var_c * 5;//warks

var_d=Test(*var_c,var_b,*var_c,var_b);

*var_c=999999999;

cout << *var_c << endl;//*= actual value &=memory location

int damage=0;

if(initialize==0)
{
////////player
c_player[0].c_strength=20;
c_player[0].c_endurance=15;
c_player[0].c_spirit=15;
c_player[0].chp=c_player[0].c_endurance*3;
c_player[0].mhp=c_player[0].c_endurance*3;
////////setup claw
strcpy(c_player[0].c_skill[0].name,"CLAW");

c_player[0].c_skill[0].stat_a=&c_player[0].c_strength;
c_player[0].c_skill[0].multiplier[0]=2;
////////set mult1
c_player[0].c_skill[0].stat_b=&global_one;//no *clas_stuff.bleh.moo=#
c_player[0].c_skill[0].multiplier[1]=0;
////////set mult2
c_player[0].c_skill[0].stat_c=&global_one;
c_player[0].c_skill[0].multiplier[2]=0;
////////enemy
c_enemy[0].c_strength=15;
c_enemy[0].c_endurance=12;
c_enemy[0].c_spirit=11;
c_enemy[0].chp=c_enemy[0].c_endurance*3;
c_enemy[0].mhp=c_enemy[0].c_endurance*3;
////////setup claw
strcpy(c_enemy[0].c_skill[0].name,"CLAW");

c_enemy[0].c_skill[0].stat_a=&c_enemy[0].c_strength;
c_enemy[0].c_skill[0].multiplier[0]=2;
////////set mult1
c_enemy[0].c_skill[0].stat_b=&global_one;//no *clas_stuff.bleh.moo=#
c_enemy[0].c_skill[0].multiplier[1]=0;
////////set mult2
c_enemy[0].c_skill[0].stat_c=&global_one;
c_enemy[0].c_skill[0].multiplier[2]=0;
initialize=1;
}

c_player[0].c_strength=23;

cout << "Battle!" << endl;
cout << "Round 1" << endl;

cout << "Player's HP: " << c_player[0].chp << "/" << c_player[0].mhp << endl;
cout << "Eenemy's HP: " << c_enemy[0].chp << "/" << c_enemy[0].mhp << endl;

//calc and deal physical damage

damage = Calculate_Damage( *c_player[0].c_skill[0].stat_a,c_player[0].c_skill[0].multiplier[0],
*c_player[0].c_skill[0].stat_b,c_player[0].c_skill[0].multiplier[0]);

cout << "Enemey took " << damage << " Damage!" << endl;

c_enemy[0].chp-=damage-c_enemy[0].c_endurance;

cout << "Round 2" << endl;
cout << "Player's HP: " << c_player[0].chp << "/" << c_player[0].mhp << endl;
cout << "Eenemy's HP: " << c_enemy[0].chp << "/" << c_enemy[0].mhp << endl;

}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
As you can see, this one is actually initializing the data.


c_player[0].c_skill[0].stat_a=&c_player[0].c_strength; //INITIALIZED HERE!!
c_player[0].c_skill[0].multiplier[0]=2;
////////set mult1
c_player[0].c_skill[0].stat_b=&global_one;//no *clas_stuff.bleh.moo=#
c_player[0].c_skill[0].multiplier[1]=0;
////////set mult2
c_player[0].c_skill[0].stat_c=&global_one;
c_player[0].c_skill[0].multiplier[2]=0;


The & means address of. So &c_strength makes the pointer point to the address of strength.

The problem with pointer tutorial is that they are very brief and to the pointer. The example they are showing doesn't belong to any context, and if they did, would probably be considered bad programming practice. I'm afraid only experience will make you better.

Just know that in the previous example, your stat_a,b,c can easily be an int instead of a pointer to an int. If you keep it as a pointer, you will need to initialize it before it's dereference (*stat_a).

When passing in a value to a function, where that function can possibly change the value, consider passing it by reference instead of using a pointer. I wish I could remember a few years back, if I could just remember what finally made me click and say "oh, that's how you use a pointer" :). Actually, I believe it was this book or rather...it's c counterpart:

http://www.amazon.com/gp/product/0471049980/sr=8-1/qid=1147053423/ref=sr_1_1/103-8523966-0704655?%5Fencoding=UTF8

Books aren't for everyone, but if you think it will help you then it might be worth to check it out.

Good luck!

Share this post


Link to post
Share on other sites
Thanks for your help, I ran into that noticing I forgot to actually initialize it (by calling the function I think XD). Turns out my errors in that program were just form being too tired. It works as designed now (the new program that I originally posted). Thanks for pointing out alot of my errors though, pointers are dangerous ._.

I'll take a look into that book though, thanks again for the help =3.

Share this post


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

  • Advertisement