Archived

This topic is now archived and is closed to further replies.

markdezonia

void * How do you use it right?

Recommended Posts

markdezonia    122
I need a pointer to a random type of data (void *) would be the right thing to use right? it seems I''m using wrong. How can you set green with yellow ? int green=20; int blue =20; void *yellow; //this may need to point to int , bool ,float ,ect. int * red; int main(int argc, char* argv[]) { char Text[40]; //this works fine red=&green; *red=50; //how do I get this to work, I need yellow to be void * yellow=&green; //ok *yellow=(void *)50; //error C2100: illegal indirection //how do I fix it. It does needs to be a void* so it can point to floats and bools too. sprintf(Text, "%u", green); //50 good printf(Text); sprintf(Text, "%u", blue); //? needs to be 50 too. printf(Text); return 0; }

Share this post


Link to post
Share on other sites
Ready4Dis    180
int green=20;
int blue =20;
void *yellow; //this may need to point to int , bool ,float ,ect.
int * red;

int main(int argc, char* argv[])
{
char Text[40];


//this works fine
red=&green;
*red=50;

//how do I get this to work, I need yellow to be void *
yellow=&green; //ok

*(int*)yellow=50;

//how do I fix it. It does needs to be a void* so it can point to floats and bools too.

sprintf(Text, "%u", green); //50 good
printf(Text);
sprintf(Text, "%u", blue); //? needs to be 50 too.
printf(Text);


return 0;
}

Share this post


Link to post
Share on other sites
felisandria    739
First off, let's talk about what void * is. It's a pointer. Just like any other pointer, trying to set it explicitly to a value is going to make bad things happen. The reason we can use void * as a type is the fact that all pointers are the same size . That's important to grasp. The data they point to can be all sorts of different sizes, but the pointer , the little set of numbers that gives the actual physical memory address , is always the same size, actual size dependent on OS. K?

That being said...

If you want to put an explicit value into the pointer you're going to have to allocate the space first, then set the pointer to it, like so (or at least close to like so, I don't have time to check my code right this second).

void *yellow;
(int*)yellow = new int;
(int)(*yellow) = 50;

You probably don't need some of that casting but it's always nice.

You can't do (void *)50 because 50 is not able to act as a variable. It must be a value. 50 cannot be the pointer to anything, except maybe the physical address 50, which is so low as to be in dangerous territory most likely.

If you want to set yellow to be a pointer to green, it would be something like this.

int green;
void *yellow;
yellow = (void*)(&green);

Does that help?

Why? It's a type, just like any other type. It's sort of an indeterminate type, which can make it more useful for things like i/o buffers where you don't particularly care what the data is, just that you have some. Some people insist on using char * instead of void* because it's "less ambiguous", but I figure that if you're using a char* like you would a void* you kind of threw the whole "lack of ambiguity" thing out the window a while ago.

-fel

[edited by - felisandria on September 3, 2003 4:48:58 PM]

Share this post


Link to post
Share on other sites
markdezonia    122
Man I had been trying to figurer that out for hours.
thanks Ready4Dis and felisandria

I have hundreds of sprite;
the numbers are how the sprites act when licked;
***I need another array for what type of data will change;***
and another for how it will change;

then it needs to be linked to varying classes of data
so it can be understood for AI statistics like the class below




TYPE_ORDINANCE[0]=3;
TYPE_ORDINANCE[1]=3;
TYPE_ORDINANCE[2]=2;
TYPE_ORDINANCE[3]=1;
TYPE_ORDINANCE[4]=2;
TYPE_ORDINANCE[5]=1;
TYPE_ORDINANCE[6]=3;
TYPE_ORDINANCE[7]=2;
TYPE_ORDINANCE[8]=1;
TYPE_ORDINANCE[9]=2;
TYPE_ORDINANCE[10]=1;
TYPE_ORDINANCE[11]=2;
TYPE_ORDINANCE[12]=1;
TYPE_ORDINANCE[13]=3;
TYPE_ORDINANCE[14]=2;
TYPE_ORDINANCE[15]=1;
TYPE_ORDINANCE[16]=3;
TYPE_ORDINANCE[17]=2;
TYPE_ORDINANCE[18]=1;
TYPE_ORDINANCE[19]=3;
TYPE_ORDINANCE[20]=2;
TYPE_ORDINANCE[21]=1;
TYPE_ORDINANCE[22]=3;
TYPE_ORDINANCE[23]=2;
TYPE_ORDINANCE[24]=1;
TYPE_ORDINANCE[25]=3;
TYPE_ORDINANCE[26]=2;
TYPE_ORDINANCE[27]=1;
TYPE_ORDINANCE[28]=2;
TYPE_ORDINANCE[29]=1;
TYPE_ORDINANCE[30]=3;
TYPE_ORDINANCE[31]=2;
TYPE_ORDINANCE[32]=1;
TYPE_ORDINANCE[33]=2;
TYPE_ORDINANCE[34]=1;
TYPE_ORDINANCE[35]=2;
TYPE_ORDINANCE[36]=1;
TYPE_ORDINANCE[37]=2;
TYPE_ORDINANCE[38]=1;
TYPE_ORDINANCE[39]=2;
TYPE_ORDINANCE[40]=1;
TYPE_ORDINANCE[41]=2;
TYPE_ORDINANCE[42]=1;
TYPE_ORDINANCE[43]=3;
TYPE_ORDINANCE[44]=2;
TYPE_ORDINANCE[45]=1;
TYPE_ORDINANCE[46]=2;
TYPE_ORDINANCE[47]=1;
TYPE_ORDINANCE[48]=2;
TYPE_ORDINANCE[49]=1;
TYPE_ORDINANCE[50]=2;
TYPE_ORDINANCE[51]=1;
TYPE_ORDINANCE[52]=2;
TYPE_ORDINANCE[53]=1;
TYPE_ORDINANCE[54]=3;
TYPE_ORDINANCE[55]=2;
TYPE_ORDINANCE[56]=1;
TYPE_ORDINANCE[57]=2;
TYPE_ORDINANCE[58]=1;
TYPE_ORDINANCE[59]=3;
TYPE_ORDINANCE[60]=2;
TYPE_ORDINANCE[61]=1;
TYPE_ORDINANCE[62]=2;
TYPE_ORDINANCE[63]=1;
TYPE_ORDINANCE[64]=2;
TYPE_ORDINANCE[65]=1;
TYPE_ORDINANCE[66]=2;
TYPE_ORDINANCE[67]=1;
TYPE_ORDINANCE[68]=2;
TYPE_ORDINANCE[69]=1;
TYPE_ORDINANCE[70]=3;
TYPE_ORDINANCE[71]=2;
TYPE_ORDINANCE[72]=1;
TYPE_ORDINANCE[73]=3;
TYPE_ORDINANCE[74]=3;
TYPE_ORDINANCE[75]=2;
TYPE_ORDINANCE[76]=1;
TYPE_ORDINANCE[77]=3;
TYPE_ORDINANCE[78]=2;
TYPE_ORDINANCE[79]=1;
TYPE_ORDINANCE[80]=2;
TYPE_ORDINANCE[81]=1;
TYPE_ORDINANCE[82]=3;
TYPE_ORDINANCE[83]=2;
TYPE_ORDINANCE[84]=1;
TYPE_ORDINANCE[85]=2;
TYPE_ORDINANCE[86]=1;
TYPE_ORDINANCE[87]=2;
TYPE_ORDINANCE[88]=1;
TYPE_ORDINANCE[89]=2;
TYPE_ORDINANCE[90]=1;
TYPE_ORDINANCE[91]=2;
TYPE_ORDINANCE[92]=1;
TYPE_ORDINANCE[93]=3;
TYPE_ORDINANCE[94]=2;
TYPE_ORDINANCE[95]=1;
TYPE_ORDINANCE[96]=2;
TYPE_ORDINANCE[97]=1;

class People_Data
{
public:
bool Incentive_ChildGov;
bool Disincentive_ChildGov;
float Incentive_Marriage;
float Incentive_Child;

float Bonus_child;
int Child_Births;
int Child_BaringMax;
int Death_Total;
int Live_Total;

bool Currency;
float Tax_Income1;
float Tax_Income2;
float Tax_Income3;
float Degree_of_Wealth [100];

bool Free_Trade;
float Tax_Sale;


bool Banking;
float Loan_percent;
float Savings;



bool Privatize;
float Tax_Property;

bool corporation;
float Corporate_Tax;
float stock;

float Investment;
float Research;
bool Monopolies;

int Policeman;
float Police_budget;
int Criminal_Investigation_man;
float Criminal_Investigation_budget;

float Corruption;
float Crime;

bool Government_Warfare;
float Unemployed;
float Unemployment_Check;
int Unemployment_Check_NO_Months;
float Child_Suppot_Check;
float Child_Suppot_Check_NO_Months;
float Warfare_Cost;

int Retiring_Age;
float Tax_Free_Investment_Retirement_Pay;
float Government_Retirement_Pay;
float Retiring_Cost;

bool Social_Securities;
float Social_Securities_Income_Level;
float Hospital_Coverage;
float Medicare_Coverage;
float Medicare_degree;
float Dental_Coverage;
float Mental_illness;

bool Disabilities;
float Disabilities_Degree;
float Disability_Securitie;

bool Medical_Law;
float Employment_Hospital;
float Employment_Medicare;
float Employment_Medicare_degree;
float Employment_Dental;
float Injury_Securitie;

bool Educational;
float Teachers;
bool Teach_Morals;
bool Work_Studies;
float Employer_Compensation;
float Effectiveness;
float Ave_Degree_of_education;
float Degree_of_Education [20];

bool Health_Law;
int Food_Health_degree;
int Drug_Health_degree;
float Ave_Degree_of_Age;
float Degree_of_Age [120];

bool Subsidized;
float Housing_degree;

float Housing_degree_Income_Level;
float Life_Support;
float Sanitation;
float Farming;

bool Grants;
float G_Research;
float G_Educational;

int Infant;
int Youth;
int Teen;
int Married;
int Single;

float GDP_PC;
float GDP_PC_Luxuries;
float GDP;
float GDP_Luxuries;
float Years;
void People();
void Intil_People();

};
People_Data PD;



[edited by - markdezonia on September 3, 2003 5:35:19 PM]

Share this post


Link to post
Share on other sites
LessBread    1415
One point of correction in regards to fels explanation - she wrote that a pointer gives the actual physical memory address - that isn''t always true, in fact it''s more likely that the pointer gives the virtual memory address. That would be the case on Windows for example. At any rate, whether physical or virtual, pointers still behave the same.

Share this post


Link to post
Share on other sites
C-Junkie    1099
its a detail he doesn''t need to know. the point of a VM is to make you think you''re writing on a machine only your program runs on.

and fel,
(int)(*yellow) = 50;
should be
*((int *)yellow) = 50;

the first, shouldn''t work. (afaik)

Share this post


Link to post
Share on other sites
quote:
Original post by felisandria
The reason we can use void * as a type is the fact that all pointers are the same size . That''s important to grasp. The data they point to can be all sorts of different sizes, but the pointer , the little set of numbers that gives the actual physical memory address , is always the same size, actual size dependent on OS. K?

The only gripe I have with this is member function pointers, as they are not necissarily the same size in C++. In fact, they can, in some common implementations, be 3 times the size of a "regular" pointer (depending on how polymorphism is tackled), or they can even be the same size as a "regular" pointer (usually done by implementing them as a pointer to a dummy function taking a pointer to the object type, which in turn will either call the function statically or dynamically). Conversions from member function pointers to void pointers is not allowed.

Share this post


Link to post
Share on other sites
LessBread    1415
quote:
Original post by C-Junkie
its a detail he doesn''t need to know. the point of a VM is to make you think you''re writing on a machine only your program runs on.



So you think it''s better to give him faulty information? And what puts you in a position to determine what details he needs to know? Do you think such a detail will confuse him? And isn''t that attitude condescending?

Share this post


Link to post
Share on other sites