Can you return an Array ???

Started by
17 comments, last by exodus7 20 years, 10 months ago
Hi Was just wondering if it''s possible to pass and return the contents of everyting in a array.
there is only one way
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
My HomepageSome shoot to kill, others shoot to mame. I say clear the chamber and let the lord decide. - Reno 911
Could you clarify that question a little bit
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.
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]
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.
[s]--------------------------------------------------------[/s]chromecode.com - software with source code
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]
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.
Brianmiserere nostri Domine miserere nostri
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
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]
Brianmiserere nostri Domine miserere nostri

This topic is closed to new replies.

Advertisement