1)Can we pass static array defined in one function( say fun1() ) to say fun2() ? If yes, then what will be actual and formal parameters ?
2)If static array can be passed as argument then how to do it in case of recursive function?
P.S I am using C
1)Can we pass static array defined in one function( say fun1() ) to say fun2() ? If yes, then what will be actual and formal parameters ?
2)If static array can be passed as argument then how to do it in case of recursive function?
P.S I am using C
Yes, you can pass static array defined in function to another function. Actual parameter is static array but formal parameter is non-static array as if it's static doesn't makes sense(Static arrays are allocated memory at compile time and the memory is allocated on the stack).
And in case of recursive function as static array is in Stack it gets gets updated in recursive calls(if we update it) its scope is lifetime of program.
#include <bits/stdc++.h>
using namespace std;
void fun2(int arr[],int i){
if(i==3)return ;
arr[0]=i;
fun2(arr,i+1);
}
void fun1(){
static int sarr[]={99};
cout<<sarr[0]<<endl;
fun2(sarr,0);
cout<<sarr[0]<<endl;
}
int main() {
fun1();
return 0;
}
Output :
99
2