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

4
  • 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. Commented Mar 27, 2016 at 7: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: stackoverflow.com/questions/27755446/… Commented Mar 27, 2016 at 8:05
  • Please supply some code. Commented Mar 27, 2016 at 8: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. Commented Mar 27, 2016 at 9:27

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

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.
atleast google before commenting link link
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 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."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.