0
How can i return an array in a function?
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.
+ 6
Yeah, but it's not necessarily wise to return a vector, because that's a lot of copying, isn't it?
+ 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);
+ 3
HonFu Apparently returning a vector is fine https://stackoverflow.com/questions/15704565/efficient-way-to-return-a-stdvector-in-c
+ 2
Or, in modern C++, you can use std::vector, which can be returned.
+ 2
Set your return type as pointer to integer...
It acts as a one dimensional array...
+ 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;
}
+ 2
SYNTAX
return identifier;
+ 2
foo(int
arr);
+ 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....
+ 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;
}
+ 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.
0
Can you give me an example?
0
The programs i am writing still doesn't work can i send here the code for make you check it?
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.
0
https://code.sololearn.com/cNZC5zfg1Poq/?ref=app
i don't know what's wrong here
0
Vlad Serbu i don't studied vectors yet so i can't use this
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..
0
Jayakrishna how can i fix the variance?
0
Jayakrishna i fixed that but in my editor i am still having some problems(dev c++)