just a quickie,
I have no reference books with me at the mo.
1. Is it possible to return an array from a function or can you only return a single value?
thanks.
just a quickie,
I have no reference books with me at the mo.
1. Is it possible to return an array from a function or can you only return a single value?
thanks.
if the array is declared locally, no.
if it was declared elsewhere, yes.
if it is dynamically allocated locally and not freed, yes.
if the array is declared locally and static, yes.
i think i know how to solve this problem using pass by reference but i was wondering if you can solve the problem this kinda way.
I just want to pass an array into a function add 1 to each of the elements then return the contents of the array into another array.
Any help more than appreciated!Code:// function that adds 1 to every element of an array. #include<iostream> using namespace std; int[] function(int array[],count); int main(void) { int array[5]={1,2,3,4,5}; int array2[]=function(array,(sizeof array[]/sizeof array[0])); return(0); } int[] function(int array[],count); { for(int x=0; x<count; x++) { array[x]+1; } return array[]; }
true, true Salem.
yes you can return it you can return nearly everthing
C++ Makes you Feel Better
"Gravity connot be held reponsible for people falling in love"--Albert Einstein
(sizeof array[]/sizeof array[0]) is a strange way to get 5 but so be it.
Seems to be fine the way you did it but I always use pointers instead of []. doesn't really matter I suppose.
i cant get the program to work . the compiler seems unhappy with my function prototype . can anyone tell me what i have done wrong please.
C:\Program Files\Microsoft Visual Studio\MyProjects\day5\main.cpp(6) : warning C4091: '' : ignored on left of 'int' when no variable is declared
C:\Program Files\Microsoft Visual Studio\MyProjects\day5\main.cpp(6) : error C2143: syntax error : missing ';' before '['
C:\Program Files\Microsoft Visual Studio\MyProjects\day5\main.cpp(6) : error C2143: syntax error : missing ';' before '['
C:\Program Files\Microsoft Visual Studio\MyProjects\day5\main.cpp(12) : error C2065: 'function' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\day5\main.cpp(12) : error C2059: syntax error : ']'
C:\Program Files\Microsoft Visual Studio\MyProjects\day5\main.cpp(17) : warning C4091: '' : ignored on left of 'int' when no variable is declared
C:\Program Files\Microsoft Visual Studio\MyProjects\day5\main.cpp(17) : error C2143: syntax error : missing ';' before '['
C:\Program Files\Microsoft Visual Studio\MyProjects\day5\main.cpp(17) : error C2143: syntax error : missing ';' before '['
C:\Program Files\Microsoft Visual Studio\MyProjects\day5\main.cpp(18) : error C2143: syntax error : missing ';' before '{'
C:\Program Files\Microsoft Visual Studio\MyProjects\day5\main.cpp(18) : error C2447: missing function header (old-style formal list?)
Error executing cl.exe.
do this instead
Code:// function that adds 1 to every element of an array. #include<iostream> using namespace std; int *function(int *array,count); int main(void) { int array[5]={1,2,3,4,5}; int *array2 = function(array, 5); return(0); } int *function(int *array, count); { for(int x=0; x<count; x++) { array[x]+1; } return array; }
array are passed by reference, you can simply modify the original if need be. This should also work:
Code:void myfunc( int* array, int size ) { for( int i = 0; i < size; i++ ) { array[ i ] += 1; } } void main( ) { int original[] = { 1, 2, 3, 4, 5 }; myfunc( original, 5 ); }
of course he already is modifying the original but he seemed to want the thing referenced by a second pointer. either way the whole thing's silly!
Thanks guys,
The penny has just dropped as to the importance of pointers
again . So having a pointer as a return type you could also return
an object . Is that correct?
No, because you are returning the pointer and not any object. But if the pointer points to the object and the object exists, then you can access it.The penny has just dropped as to the importance of pointers
again . So having a pointer as a return type you could also return
an object . Is that correct?
It is possible to return a local array. Wrap it in a struct
Not much point, though.Code:struct arr { int a[5]; }; arr foo() { arr a; return a; } int main() { arr b = foo(); }