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
static arrays are typicaly not allocated on the stack. autoarrays are. Actually, it is up to the compiler/linker suite where stuff is being put. The storage class just puts some requirements there - how they are fulfilled is up to the compiler.statically allocated arrays. The first one is even completely C++, which makes it invalid here. Then go back to the OP question, then realize the "C" tag, then review your answer. I might then re-think my vote. (The second link even helps contradicting what you say: "Statically sized arrays declared within a function (and not declared with the 'static' keyword; this would make them a global) are going to be put on the stack."
staticis a C keyword, and it seems to me you mean something else when you use this word. Maybe what you ask is related to: stackoverflow.com/questions/27755446/…