About variable type switching..
I am stuck on a certain point in making a program. That would be using pointers to pull diffrent strings from an array, while still being interchangable with a "char" type.
For example..
Code:
void feats()
{
int x;
char feat1[] = "Alertness";
char feat2[] = "Ambidexterity";
char feat3[] = "Armor Proficency(small)";
char feat4[] = "Armor Proficency(medium)";
char feat5[] = "Armor Proficency(large)";
void *pf1 = feat1;
void *pf2 = feat2;
void *pf3 = feat3;
void *pf4 = feat4;
void *pf5 = feat5;
srand(time(NULL));
x = rand()%5;
if(x == 1)
{
Feat1 = pf1;
}
else if(x == 2)
{
Feat1 = pf2;
}
else if(x == 3)
{
Feat1 = pf3;
}
else if(x == 4)
{
Feat1 = pf4;
}
else
{
Feat1 = pf5;
}
return;
}
I have absolutely no idea how to make this accept the pointer's value. Any help would be greatly appriacated..
Re: About variable type switching..
Quote:
Originally posted by Dragonlord
I am stuck on a certain point in making a program. That would be using pointers to pull diffrent strings from an array, while still being interchangable with a "char" type.
For example..
void feats()
{
int x;
char feat1[] = "Alertness";
char feat2[] = "Ambidexterity";
char feat3[] = "Armor Proficency(small)";
char feat4[] = "Armor Proficency(medium)";
char feat5[] = "Armor Proficency(large)";
void *pf1 = feat1;
void *pf2 = feat2;
void *pf3 = feat3;
void *pf4 = feat4;
void *pf5 = feat5;
srand(time(NULL));
x = rand()%5;
if(x == 1)
{
Feat1 = pf1;
}
else if(x == 2)
{
Feat2 = pf2;
}
else if(x == 3)
{
Feat3 = pf3;
}
else if(x == 4)
{
Feat4 = pf4;
}
else
{
Feat5 = pf5;
}
return;
}
I have absolutely no idea how to make this accept the pointer's value. Any help would be greatly appriacated..
you need to typecast when you assign a array name to a void pointer. since data type for array is char so you need to do like this: Feat1 = (char *)pf1;
do the same for Feat2..Feat3..
Earth_King
Re: Re: About variable type switching..
Quote:
Originally posted by Earth_King
you need to typecast when you assign a array name to a void pointer. since data type for array is char so you need to do like this: Feat1 = (char *)pf1;
do the same for Feat2..Feat3..
Earth_King
Alright. I've tried to do that, but I believe the problem doesn't lie with char[] -> void, I believe the problem is in this: char[] -> void -> char.
error C2440: '=' : cannot convert from 'char *' to 'char'
or
error C2440: '=' : cannot convert from 'void *' to 'char'
The question I had was on how to get these to accept each other. I can't cout the pointers (nasty), nor can I typecast them to print out anything but strange characters and then have the program die.
I am thinking that I have to use the array variables themselves in the cout, but even then I can't randomize the variables..
[edit: Code above..]