Question: Why the below gives different Outputs?

```
 #include<iostream>

 using namespace std;

 int* sqcb(int n)
 {
     int a[3];
     a[0]=n*n;
     a[1]=n*n*n;
     a[2]=786;
     return a;  
 }

 int main()
 {
     int a,c;int const *b;
     cout<<"Enter an Integer:";
     cin>>a;

     b=sqcb(a);

     cout<<*(b)<<"\t"<<*(b+1)<<"\t"<<*(b+2)<<"\t"; // <- this

     cout<<*(b)<<"\t";   // <- and these
     cout<<*(b+1)<<"\t"; // 
     cout<<*(b+2)<<"\t"; // 
 }

```

Answer: 
```
int* sqcb(int n)
{
  int a[3];
  a[0]=n*n;
  a[1]=n*n*n;
  a[2]=786;
  return a;  
}

```

You are returning a pointer to a local variable, which is [naughty](http://en.wikipedia.org/wiki/Undefined_behavior).
Your code in `sqcb` causes undefined behavior: your return the address of the array variable `a` which gets invalid after the function returns. You **may change your `sqcb`function** in one of the following ways as to get the equal outputs you expect.

Using a global variable
-----------------------

```
int g_a[3]; // global variable, this clutters the global namespace and
            // is accessible (and therefore mis-usable) from everywhere and
            // a lot of more problems

int* sqcb(int n)
{
    g_a[0]=n*n;
    g_a[1]=n*n*n;
    g_a[2]=786;
    return g_a;
}

```

Switch to a static variable
---------------------------

```
int* sqcb(int n)
{
    static int a[3]; // static variable
                     // this may causes problems in multi-threaded environments
    a[0]=n*n;
    a[1]=n*n*n;
    a[2]=786;
    return a;
}

```

But I'd **prefer rethinking** the problem and change the whole program.