Jump to content
  • Advertisement

Archived

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

exodus7

Can you return an Array ???

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

Hi Was just wondering if it''s possible to pass and return the contents of everyting in a array.

Share this post


Link to post
Share on other sites
Advertisement
usually you return it through itself by passing it by reference. like this:
void func(int* &array)
{
//do stuff to array
}
or this probably will work also
int * func()
{
int * a;

return a;
}

My Homepage

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Could you clarify that question a little bit

Share this post


Link to post
Share on other sites
Julio, you don''t need to pass an array by reference. In fact, that''s remarkably pointless. The second one you have will definitely not work, since you''d have to allocate the array using new, and then rely on the calling code to delete the array it gets. That is a very, very bad idea.

Share this post


Link to post
Share on other sites
Here is how you work with an array:

    
void ModifyArray(int* array, int size)
{
for (int i=0; i<size; i++)
do something to array[i];

// since the array was passed as a pointer,

// modifying it in here will affect the array

// in the calling code.

}

That's it.


[edited by - micepick on May 26, 2003 1:05:00 PM]

Share this post


Link to post
Share on other sites
If you doesn''t concern about speed, you can pass a whole struct containg an array:

struct Wazza
{
int data[100];
};

void SomeThing( Wazza whole_struct );




____________________ ____ ___ __ _
Enselic''s Corner - My site. Go test my game Spatra and see if you can beat it onto the Official Spatra Top 10.
CodeSampler.com - Great site with source for specific tasks in DirectX and OpenGL.

Share this post


Link to post
Share on other sites
Or, if you don't want to affect the array that's passed in:

            
int* ArrayOp(int* out, const int* in, int size)
{
// assume the array 'out' was already allocated


for (int i=0; i<size; i++)
out[i] = some_operation(in[i]);

// have the function return a pointer to the output as well,

// just for convenience

return out;
}

Note that this is analogous to functions like memcpy, which are declared thus:

void* memcpy(void *dest, const void *src, size_t size)


[edited by - micepick on May 26, 2003 1:17:25 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by exodus7
Hi
Was just wondering if it''s possible to pass and return the contents of everyting in a array.


Yes, but I cannot think of a reason you would ever want to. Normally you pass/return a pointer, or use references.

Share this post


Link to post
Share on other sites
quote:

Yes, but I cannot think of a reason you would ever want to. Normally you pass/return a pointer, or use references.


i can. i once saw a function that takes in an error message (directX, to be exact) and returns the an array of what error it is. heres what it looked like:

  
char* GetErrorMsg(HRESULT hr)
{
switch(hr)
{
case DDERR_LOSTDEVICE:
return "DDERR_LOSTDEVICE";
...
}
}

btw i dont even think that error msg exists i was just using it as an example



doh, nuts. Mmmm... donuts
My website

Share this post


Link to post
Share on other sites
quote:
Original post by brassfish89
i can. i once saw a function that takes in an error message (directX, to be exact) and returns the an array of what error it is. heres what it looked like:

char* GetErrorMsg(HRESULT hr)
{
...




Note the return type: char *. Even though the string (char array) is hard-coded, the function is returning a pointer to it, ie. the address of where it sits in memory at runtime.

[edited by - BriTeg on May 26, 2003 12:47:10 AM]

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!