0

How can i return an array in a function?

9th Mar 2020, 4:19 PM
Massimiliano Messina
Massimiliano Messina - avatar
40 ответов
+ 11
You can not, at least not raw arrays, because they decay to ordinary pointers when you pass them as arguments or return value. The regular way is to pass the array as a pointer (or reference) and fill it from the function. So you'd write a void function and not return anything.
9th Mar 2020, 4:41 PM
HonFu
HonFu - avatar
+ 6
Yeah, but it's not necessarily wise to return a vector, because that's a lot of copying, isn't it?
9th Mar 2020, 4:46 PM
HonFu
HonFu - avatar
+ 3
void f(int *array, int size) { for(int i=0; i<size; ++i) array[i] = (array[i]+5)*2; } Here a random example. Call it for example like this: int n[] = {1, 4, 7}; f(n, 3);
9th Mar 2020, 4:49 PM
HonFu
HonFu - avatar
9th Mar 2020, 4:53 PM
Vlad Serbu
Vlad Serbu - avatar
+ 2
Or, in modern C++, you can use std::vector, which can be returned.
9th Mar 2020, 4:44 PM
Vlad Serbu
Vlad Serbu - avatar
+ 2
Set your return type as pointer to integer... It acts as a one dimensional array...
10th Mar 2020, 3:14 AM
Bala Krishnan
Bala Krishnan - avatar
+ 2
void f(int *array, int size) { for(int i=0; i<size; ++i) array[i] = (array[i]+5)*2; } Call it for example like this: int n[] = {1, 4, 7}; f(n, 3); The function prototype should match with function definition arguments.. void input(int array[] , int size); void input(int array[], int size) { ... //logic } I mean passing array to array will store starting addresses, [ so will works through fetching values through continuous addresses and that's why, they are done as pass by reference]. Pointers stores addresses, so if you pass array to pointer, it stores first element address so which is same.. You know int *ptr=&i; int*ptr; program- #include<stdio.h> int fun(int arr2[]) { return arr2[4] = 5; } int main() { int arr[5] = {1,2,3,4},i; clrscr(); printf("\nBefore"); for(i=0;i<5;i++) { printf("\n%d",arr[i]); } arr[5] = fun(arr); printf("\n\nAfter"); for(i=0;i<5;i++) { printf("\n%d",arr[i]); } getch(); return 0; }
10th Mar 2020, 6:20 AM
Venugopal Kousik Ambatipudi
Venugopal Kousik Ambatipudi - avatar
+ 2
SYNTAX return identifier;
10th Mar 2020, 9:49 AM
Himanshu Sharma
Himanshu Sharma - avatar
+ 2
foo(int arr);
25th Mar 2023, 7:13 PM
Farhan Ali
+ 1
Massimiliano Messina Since Arrays are passed by reference, no need to return array.. [You can continue, as like it is returned already]. Else Other way return like pointer return *arrayName; But this is same as doing with void function....
9th Mar 2020, 4:45 PM
Jayakrishna 🇮🇳
+ 1
#include<stdio.h> int fun(int array2[]) { return array2[4] = 5; } int main() { int array[5] = {1,2,3,4},i; // clrscr(); printf("\nBefore"); for(i=0;i<5;i++) { printf("\n%d",array[i]); } array[5] = fun(array); printf("\n\nAfter"); for(i=0;i<5;i++) { printf("\n%d",array[i]); } // getch(); return 0; }
10th Mar 2020, 6:35 AM
Learner
+ 1
int * function() { int *A = new int[10]; return A; } note: at the end use delete operetor on the pointer that you store the Array to realese the memory.
10th Mar 2020, 4:53 PM
Ali Moammeri
Ali Moammeri - avatar
0
Can you give me an example?
9th Mar 2020, 4:45 PM
Massimiliano Messina
Massimiliano Messina - avatar
0
The programs i am writing still doesn't work can i send here the code for make you check it?
9th Mar 2020, 4:54 PM
Massimiliano Messina
Massimiliano Messina - avatar
0
Vlad Serbu, ah I see, so it's after all not so risky to behave like it was Python because of the compiler optimizations? Guess it would also depend on if the poster is thinking of the higher array-like types or rather C-arrays.
9th Mar 2020, 4:56 PM
HonFu
HonFu - avatar
0
9th Mar 2020, 5:11 PM
Massimiliano Messina
Massimiliano Messina - avatar
0
Vlad Serbu i don't studied vectors yet so i can't use this
9th Mar 2020, 5:13 PM
Massimiliano Messina
Massimiliano Messina - avatar
0
Massimiliano Messina Make these changes av=average(a,size); v=variance( av, a, size); Line 30, cout<<array[i]; //typo not a[i] And prototype of average return type not matching function return, one is double and other float, make to match both as float.. v=v+(av-array[i])-(av-array[i]) //v=v+0; This always zero so returns v value only..
9th Mar 2020, 5:38 PM
Jayakrishna 🇮🇳
0
Jayakrishna how can i fix the variance?
9th Mar 2020, 6:22 PM
Massimiliano Messina
Massimiliano Messina - avatar
0
Jayakrishna i fixed that but in my editor i am still having some problems(dev c++)
9th Mar 2020, 6:27 PM
Massimiliano Messina
Massimiliano Messina - avatar