我知道test(m,t)的t如果在调用之前就申请空间的话不会有问题,但是我的程序的申请空间的大小要在test函数里面才能确定

解决方案 »

  1.   

    void test( int &num, int* &p )  //指针的引用
    {
    num = 3;
    p = new int [num];
    for( int i=0; i<num; i++ )
    {
    p[i] = i;
    }
    }
      

  2.   

    void test( int &num, int* &p )  //指针的引用
    这个我当然想过了,可是编译器总是说有问题。MD
      

  3.   

    E:\Test\TestDlg.cpp(174) : error C2528: '<Unknown>' : pointer to reference is illegal
    E:\Test\TestDlg.cpp(177) : error C2440: '=' : cannot convert from 'int *' to 'int ** '
            Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    E:\Test\TestDlg.cpp(180) : error C2440: '=' : cannot convert from 'int' to 'int *'
            Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    E:\Test\TestDlg.cpp(201) : error C2664: 'test' : cannot convert parameter 2 from 'int *' to 'int ** '
            Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    Error executing cl.exe.Test.exe - 4 error(s), 0 warning(s)
      

  4.   

    有没有搞错啊,你这种写法是不对的,虽然传的是指针,但实际是把指针作为值参数来传了,因此你得到的结果不正确,应该传指针的指针,如下:
    void test( int &num, int **p )
    {
    num = 3;
    *p = new int [num];
    for( int i=0; i<num; i++ )
    {
    (*p)[i] = i;
    }
    }
    void CTestDlg::OnOk() 
    {
    // TODO: Add your control notification handler code here
    int m;
    int *t=NULL; test(m,&t);//此时应传指针的地址
    }
      

  5.   

    不会吧。这里编译怎么没问题啊。
    或者改成这样:
    void test( int &num, int** p )   
    {
    num = 3;
    *p = new int [num];
    for( int i=0; i<num; i++ )
    {
    p[i] = i;
    }
    }
      

  6.   

    //那就这样把void test( int &num, int **p )
    {
    num = 3;
    *p = new int [num];
    for( int i=0; i<num; i++ )
    {
    (*p)[i] = i;
    }
    }
    void CTestDlg::OnOk() 
    {
    // TODO: Add your control notification handler code here
    int m;
    int *t=NULL; test(m,&t);
    delete[]t;

    }
      

  7.   

    不会吧。这里编译怎么没问题啊。
    或者改成这样:
    void test( int &num, int** p )   
    {
    num = 3;
    *p = new int [num];
    for( int i=0; i<num; i++ )
    {
    p[i] = i;
    }
    }
      

  8.   

    或者用指针的指针,或者用指针的引用,我用vc6 编译了:
    void test( int &num, int* &p )  //指针的引用
    {
    num = 3;
    p = new int [num];
    for( int i=0; i<num; i++ )
    {
    p[i] = i;
    }
    }
    ///没有什么问题呀!!
      

  9.   

    MMD,原来我的指针的引用写错了,写成了void test3( int &num, int & *p )
    害的我白花了半天的时间.
    散分。