Can't return array from a function, right?
Let me get this straight, you can''t return an array from a function, correct? You could return a single element from an array, but not the entire array, right? The only way to access an array from a function is by pointers or references is what I''m thinking.
Rhino2876
that''s correct.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
In C++ you can return a vector:
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions ]
Thanks to Kylotan for the idea!
template < typename T >std::vector< T > Pack( T & data1, T & data2 ){ std::vector< T > v; v.push_back( data1 ); v.push_back( data2 ); return v;}
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions ]
Thanks to Kylotan for the idea!
Sure you can return an array from a function. An array is just a pointer to memory (address), so your function must return a pointer to a memory location.
------
If I'm incorrect, sue me, you'll get nothing
[edited by - aNonamuss on April 12, 2002 12:55:18 AM]
void main(void){ char szTempString[MAX_CHARS]; strcpy(szTempString, ChangeArray());}char *ChangeArray(void){ return "You can return an Array from a Function";}
------
If I'm incorrect, sue me, you'll get nothing
[edited by - aNonamuss on April 12, 2002 12:55:18 AM]
------Shop for the Lowest Price!Then, Check a Reseller's Rating Before You Purchase!
quote: Original post by aNonamuss
Sure you can return an array from a function. An array is just a pointer to memory (address), so your function must return a pointer to a memory location.
Sure, the problem comes when you do something like this:
char *function(){ char buffer[256]; // ... // ... do something with buffer, fill it with what you want // ... return buffer;}int main(){ char *ptr_to_buffer = function(); cout<<ptr_to_buffer[0]; // SHIT!!! "buffer" was destroyed! Ah well, lets just let the program crash return 0;}
You get the idea...
Death of one is a tragedy, death of a million is just a statistic.
If at first you don't succeed, redefine success.
Actually just the permission to WRITE to that memory was removed, you can still read its contents just fine.
-----------------------
"When I have a problem on an Nvidia, I assume that it is my fault. With anyone else''s drivers, I assume it is their fault" - John Carmack
-----------------------
"When I have a problem on an Nvidia, I assume that it is my fault. With anyone else''s drivers, I assume it is their fault" - John Carmack
-----------------------"When I have a problem on an Nvidia, I assume that it is my fault. With anyone else's drivers, I assume it is their fault" - John Carmack
quote: Original post by aNonamuss
Sure you can return an array from a function.
No you can''t.
quote:
An array is just a pointer to memory (address)
No it''s not.
quote:
so your function must return a pointer to a memory location.
You can do that, but a pointer isn''t an array.
"Let me get this straight, you can''t return an array from a function, correct? You could return a single element from an array, but not the entire array, right? The only way to access an array from a function is by pointers or references is what I''m thinking.''
Guys, I already acknowledged that you could use pointers to change an array. I guess what you are all telling me is that I could return the entire array with pointers. That''s cool. Well, why does this work then.
Here is something interesting that I experimented with. A function could change an array without pointers.
CONSOLE OUTPUT:
1
Hi
Press any key to continue
Notice that I actually changed the first 2 elements of c[] with the i[] array that is local to the function test. I thought that local variable do not change values in main unless if you use pointers. Strange.
Guys, I already acknowledged that you could use pointers to change an array. I guess what you are all telling me is that I could return the entire array with pointers. That''s cool. Well, why does this work then.
Here is something interesting that I experimented with. A function could change an array without pointers.
#include int test(char i[]);int main(){ char c[5] = " "; printf("\n\n%d\n\n", test(c)); printf("\n\n%s\n\n", c); return 0;}int test(char i[]){ i[0] = ''H''; i[1] = ''i''; return 1; }
CONSOLE OUTPUT:
1
Hi
Press any key to continue
Notice that I actually changed the first 2 elements of c[] with the i[] array that is local to the function test. I thought that local variable do not change values in main unless if you use pointers. Strange.
Rhino2876
You''re not passing an array out of a function there, you''re passing one in.
Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
from Mr. Obvious:
"You''re not passing an array out of a function there, you''re passing one in."
I know that. But why is the function changing the array in main? It should only change the array in the function, not the array in main. I thought that''s how local variable worked.
"You''re not passing an array out of a function there, you''re passing one in."
I know that. But why is the function changing the array in main? It should only change the array in the function, not the array in main. I thought that''s how local variable worked.
Rhino2876
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement