2

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

user6119874
  • 95
  • 2
  • 12
  • 1
    That an array is static doesn't matter, you pass it as any other array. But remember that when you pass an array to a function is decays to a pointer to its first element, so if you need the number of elements in the array you need to pass it too. – Some programmer dude Mar 27 '16 at 07:33
  • What do you call a "static" array? `static` is a C keyword, and it seems to me you mean something else when you use this word. Maybe what you ask is related to: http://stackoverflow.com/questions/27755446/reference-to-array-vs-reference-to-array-pointer – jdarthenay Mar 27 '16 at 08:05
  • Please supply some code. – Harry Mar 27 '16 at 08:40
  • You can, but you shouldn't. Avoid static data. Make the array automatic or heap allocated. If you think you need this, show your use case. – n. m. could be an AI Mar 27 '16 at 09:27

1 Answers1

0

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

Polish
  • 554
  • 1
  • 4
  • 18
  • 1
    `static` arrays are typicaly *not* allocated on the stack. `auto`arrays 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. – tofro Mar 27 '16 at 10:08
  • atleast google before commenting [link](http://stackoverflow.com/questions/2672085/c-static-array-vs-dynamic-array) [link](https://www.quora.com/Where-are-the-arrays-in-C-stored-in-a-stack-or-a-heap) – Polish Mar 27 '16 at 10:24
  • 1
    You're so right. But then *understand* what you are reading. The links you provided talk about *statically sized* arrays vs. *dynamically sized* arrays, not `static`ally *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." – tofro Mar 27 '16 at 10:39