如题:
如下显示的值为 0 ,不知为何?谢谢指点.
double *GetPoint()
{
  double d1=1;
  double d2=2;
  double *p;
  double a[2]={d1,d2};
  p=a;
  return(p);
}
void CHelloDlg::OnButton()
{
  double *p;
  double *GetPoint();
  CString str;
  str.Format("%f",*(p+1));
  AfxMessageBox(str);
}

解决方案 »

  1.   

    GetPoint()返回值指向函数内部变量a,GetPoint()运行完后会释放内存.如下可解决(全局变量):double a[2];double *GetPoint()
    {
      double d1=1;
      double d2=2;
      double *p;  a[0] = d1;
      a[1] = d2;
      p=a;  return(p);
    }void CHelloDlg::OnButton() 

      CString str;    double *p = GetPoint();  str.Format("%f",*(p+1));
      AfxMessageBox(str);
    }
      

  2.   

    1.返回临时变量的指针或引用是绝对不允许的。在函数内new,调用后记得delete.
      

  3.   

    因为a是临时变量,在其作用域完之后就被释放了。
      double *GetPoint()
    {
      double d1=1;
      double d2=2;
      double *p;
      double a[2]={d1,d2};
      p=a;
      return(p);
    }   //a被释放了改正方法1是 tdstds(蓝海小丑) ( ) 提供的使用全局变量,或者:
    double *GetPoint(double a)
    {
      return &a;
    }再调用:void CHelloDlg::OnButton()
    {
      double b;
      double* p = GetPoint(b);
    ....
    }
      

  4.   

    你的源代码又问题:p=GetPoint();后,返回结果为2.000000
      

  5.   

    请问能不能用静态变量啊?
    static double a[2]={d1,d2};