array return type

后端 未结 5 775
有刺的猬
有刺的猬 2021-01-29 15:25
#include
#include
#define MAX 30

void push(char );


char stack[MAX];
int tos=0;

int main(){
    char str[]=\"Arijit Saha\";
    char *f         


        
5条回答
  •  青春惊慌失措
    2021-01-29 16:05

    reverse is a local array. It is destroyed, when the function exits, but you return a pointer to its contents. Ideally, you should return the array by having a parameter that the function will load data into, i.e.

    void MyFuncReturnsArray(int* myArr, int n)
    {
       for(int i = 0; i < n; ++i)
          myArr[i] = i;
    }
    

    Instead of

    int* MyFuncReturnsArray()
    {
         int myArr[10];
         for(int i = 0; i < 10; ++i)
            myArr[i] = i;
         return myArr
    
    }
    

提交回复
热议问题