Jump to content
  • Advertisement

Archived

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

fireking

how to set a pointer as a parameter?

This topic is 5457 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''s the deal ive got two global pointers: float *myverts; float *mytexcoords; it doesnt point to anything yet... i have a function, that builds two arrays, but cant return them because there are two, so i need the programmer to pass two paramters, each should be pointers that will point to the built array''s MakeTheArrays(somestuff,myverts,mytexcoords); and the function should make myverts, and mytexcoords, point to locations of the newly built arrays...
void MakeTheArrays(int somestuff,float *wverts,float *wtexcoords)
{
    float *verts = new float[somestuff];
    float *texcoords = new float[somestuff];

    ...
    
    wverts=verts;
    wtexcoords=texcoords;
    ...
}
unfortunately this doesnt work could someone help me?

Share this post


Link to post
Share on other sites
Advertisement
You can declare your function:
void MakeTheArrays(int somestuff,float * & wverts,float * & wtexcoords)
it should work the way you have the function coded.

Share this post


Link to post
Share on other sites
thanks guys, but im not sure if my new array line is working...

float *something= new float[somethingcount];

is that the correct syntax for making the array?

like

something[0]
something[1]
...
something[25245]

thanks for the help so far, i dont know what i would do without this forum

Share this post


Link to post
Share on other sites
well i was just wondering, because the debugger shows the contents of variables (vs.net), and it shows verts, the address, and if you expand the node, it shows 0.000000 and thats it...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Please can someone explain why fireking''s original code doesn''t work? Is it not valid to pass an array to a function and modify it?? Why the hell doesn''t it work?! I''m confused...

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
Please can someone explain why fireking''s original code doesn''t work? Is it not valid to pass an array to a function and modify it?? Why the hell doesn''t it work?! I''m confused...


Because you''re PASSING the address of the array. You can therefore modify the data at this address without issue.

If you do:
void Test(int x)
{
x= 10;
}

This will NOT modify the original value passed in, similar to passing an array''s memory address. You CANNOT modify it''s memory address, just the values that it points to. When you pass it by reference, or a double pointer (I''ll show this below), you are then passing the address of the variable that stores the address in memory, so you can now modify the address in memory that it points to.


Rather than references, you can also do this:
void MakeTheArrays(int somestuff, float **wverts, float **wtexcoords)
{
float *verts = new float[somestuff];
float texcoords = new float[somestuff];

*wverts = verts;
*wtexcoords = texcoords;
}

It''s a bit more complicated looking, but achieves the same results . (This is what we had to do before references came about).

Share this post


Link to post
Share on other sites

  • 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!