+ 1

How to return address of array from a function?

can someone help me with this code.The code below has to accept input values for the array from the function 'ip' and print the array . Address of array should be passed as input to the function .This is my code and i'm getting this error([Error] invalid conversion from 'int*' to 'int' [-fpermissive]). #include<iostream> using namespace std; int ip(int *p); int main() { int a[10]; int *ptr; *ptr= ip(a); for(int i=0;i<10;i++) { cout<<*(ptr+i)<<endl; } return 0; } int ip(int*p) { for(int i=0;i<10;i++) { cin>>*(p+i); } return a; }

24th Jun 2019, 7:22 AM
Deepak Chekuri
3 Réponses
+ 2
declare your function as a function pointer int *ip(int *p), that should work
24th Jun 2019, 7:25 AM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 1
You're God bruhh💯
24th Jun 2019, 7:30 AM
Deepak Chekuri
+ 1
Actually, there's no need to mess with return type since the nature of passing an array (unlike a simple variable) to another function involves decaying the array into a pointer with the proper type (the array lose its dimension and therefore the ability to get the size no longer possible). That means, `ip(a)` is the same as `ip(&a[0])` and what is being done inside the `ip` function would directly influence the actual array in the main(). So, as a conclusion, the above code can simply be revised as #include<iostream> using namespace std; void ip(int *p); int main() { int a[10]; ip(a); for (int i = 0; i < 10; i++) { cout << *(a + i) << endl; } return 0; } void ip(int *p) { for (int i = 0; i < 10; i++) { cin >> *(p + i); } }
24th Jun 2019, 8:23 AM
To Seek Glory in Battle is Glorious
To Seek Glory in Battle is Glorious - avatar