[C++][help]Poiners structures and variables
i use these variables in my script
but for some reason out of my knowlages they give arrors
and wont drop numbers inside the variables + it wont make an
array
can anyone tell me why they wont work and tell me how to fix this
im trying to figger this out for 3 hours now and google wont rly help me T_T
thx in advance
Code:
struct stats {
int *wep;
int *dmg;
int *health;
};
void main() {
//variables used in script
stats player;
*player.health = 100;
stats enemy;
*enemy.health = 100;
player.wep = new int[6];
*player.wep = 1;
player.dmg = new int[6];
*player.dmg = 1;
enemy.wep = new int[6];
*enemy.wep = 1;
enemy.dmg = new int[6];
*enemy.dmg = 1;
//end of script
delete enemy.wep;
delete enemy.dmg;
delete player.dmg;
delete player.wep;
system("PAUSE");
}
Re: [C++][help]Poiners structures and variables
Code:
struct stats {
int wep;
int dmg;
};
void main() {
//variables used in script
int player_health = 100;
int enemy_health = 100;
struct stats player[6];
struct stats enemy[6];
player[0].wep = 1;
player[0].dmg = 1;
enemy[0].wep = 1;
enemy[0].dmg = 1;
system("PAUSE");
}
Re: [C++][help]Poiners structures and variables
Quote:
Originally Posted by
Daevius
Code:
struct stats {
int wep;
int dmg;
};
void main() {
//variables used in script
int player_health = 100;
int enemy_health = 100;
struct stats player[6];
struct stats enemy[6];
player[0].wep = 1;
player[0].dmg = 1;
enemy[0].wep = 1;
enemy[0].dmg = 1;
system("PAUSE");
}
thnx but if i want to make a new array from a struct i have
to use
struct stats to call the structure and player / enemy[6] to to make an array off al variables inside the structure??
EDIT
lol i just changed form new blood to Ultimate Member^^
sry about dubble post did new post instead of edit
Re: [C++][help]Poiners structures and variables
A struct is something that can keep track of several values, just like classes.
By making an array of structs, you generally have an array of the values within the struct that are connected with eachother.
You made an array of 6 times the struct, and you can use the values like this:
Code:
struct stats {
int wep;
int dmg;
};
void main() {
struct stats player[3];
player[0].wep = 1;
player[0].dmg = 1;
player[1].wep = 2;
player[1].dmg = 4;
player[2].wep = 3;
player[2].dmg = 2;
Here I made an array with 3 structs, all filled with wep and dmg ;).
Hope that helps.
Re: [C++][help]Poiners structures and variables
thnx for explaining
u just learned me somthing new^^
in the c++ vid i watched about structures they called the variables (array) from the structures with pointers
Re: [C++][help]Poiners structures and variables
A struct and class are the same thing. Structs are legacy and come from C, Classes were only introduced in OO languages.
On the pointer in the first example, when dereferencing you have to remember that a pointer is nothing more than a location. so to do the array we need to push the pointer to the next area. So if you wanted to use it as an array we need to do the following
(enemy+0)->health = 100; //would reference the first object in the array
(enemy+1)->health = x; // would reference the second object in the array
etc etc
When doing things this way, do remember to malloc/memset the array, as pointers are open memory.
Hope this helps (sorry bout the quick example, i gota run)
Re: [C++][help]Poiners structures and variables
the quick example was enough to understand:P
ty
Re: [C++][help]Poiners structures and variables
Ok, sorry about not editing my previous post, but just want to bump this as well.
(Once again my apologies for the quick explanation)
When using pointers as arrays, what you are doing, is that you are starting with a single memory address (your starting pointer). Now the subsequent memory can be used as and "array" of these pointers. This is why memset/malloc are important (especially malloc) you need to inform your code that you will be using the subsequent space as the same type as the first pointer. The memset is just to clear out the garbage in that area although malloc will do that as well, as malloc implicitly calls memset.
When this is done it is as good as having multiple pointers to that location in space. So diagramatically what we are doing is allocating pointer A, initially leaving open space B
_ _ _ _ _ _ _
|A|B|B|B|B|B|B|
When we then memset what we are doing is setting all the B's to A's for that section in space ending up with
_ _ _ _ _ _ _
|A|A|A|A|A|A|A|
So when we want to address the next areas in memory all we need to do with the pointer is move it one forward. So what we do is pointer+1/pointer+2 etc untill pointer+6 (in this case) which will be the last address.
Hope this is a better explanation.
Enjoy