CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2012
    Posts
    7

    How to return different array in different function?

    Excuse me, I'm a beginner in C++
    I want to calculate physics system so that I need to use large variable.

    Code:
    double multiply_matrix_RP(int u0,int u1,int u2,int u3,int u4,int u5,int u6,int u7,int u8)
    {
            //u0,u1~u8 there are material array
    	long double _Rp[9];
    	for(int i=0;i<=8;i++)
           {
    	_Rp[i]=EAI[i][0]*u0+EAI[i][1]*u1+EAI[i][2]*u2+EAI[i][3]*u3+EAI[i][4]*u4+EAI[i][5]*u5+EAI[i][6]*u6+EAI[i][7]*u7+EAI[i][8]*u8;	
    	}
    	return _Rp[9];
    }
    
    void CImp_simDlg::OnOK() 
    {
    	// TODO: Add extra validation here
    	double _Rp[9];
    	_Rp[9]=multiply_matrix_RP(Rp[0],Rp[1],Rp[2],Rp[3],Rp[4],Rp[5],Rp[6],Rp[7],Rp[8]);
    
    	//CDialog::OnOK();
    }
    But it actually have error message when it compiler.
    Please help me to solve, thank every much!!!

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: How to return different array in different function?

    Quote Originally Posted by chrishappy11 View Post
    Excuse me, I'm a beginner in C++
    I want to calculate physics system so that I need to use large variable.
    Arrays are not copyable, so you cannot return them. So forget about the way you wrote the code -- it will not work.

    There are many ways to solve the problem, the most obvious being that you pass a pointer to the first element of the array to the function, and then let the function change the array,
    Code:
    double multiply_matrix_RP(double* p, int u0,int u1,int u2,int u3,int u4,int u5,int u6,int u7,int u8)
    {
            //u0,u1~u8 there are material array
        for(int i=0; i < 9; ++i)
        {
            p[i]=EAI[i][0]*u0+EAI[i][1]*u1+EAI[i][2]*u2+EAI[i][3]*u3+EAI[i][4]*u4+EAI[i][5]*u5+EAI[i][6]*u6+EAI[i][7]*u7+EAI[i][8]*u8;	
         }
    }
    //...
    double x[9];
    //...
    multiply_matrix_rp(x, val1, val2, val3, /*....etc... */);
    Second, your code is confusing. You have _Rp, Rp, an unknown E array used in the function, etc. Where did E come from? Is _Rp and Rp supposed to be the same array? If they are, then your code will not work correctly, as you're using an uninitialized array, and doing that could cause an exception to be thrown due to invalid floating point values being used.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Jul 2012
    Posts
    7

    Re: How to return different array in different function?

    Sorry...
    In addition, There array EAI[9][9] ,Rp[9] are constant array, I declare other where.
    If I return array in another function, It seem to be irregular.

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: How to return different array in different function?

    Also note: you are doing things like:

    Code:
    double _Rp[9];
    _Rp[9]=multiply_matrix_RP(Rp[0],Rp[1],Rp[2],Rp[3],Rp[4],Rp[5],Rp[6],Rp[7],Rp[8]);
    But 9 is not a valid index for the _Rp array. The only valid indices are: 0,1,2,3,4,5,6,7,8

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: How to return different array in different function?

    Quote Originally Posted by chrishappy11 View Post
    Sorry...
    In addition, There array EAI[9][9] ,Rp[9] are constant array, I declare other where.
    If I return array in another function,
    Again, you cannot return arrays. What you are doing is something else other than returning an array. Maybe your description of what you're doing is not correct. You can pass a pointer to the first element of the array, or you can wrap the array in a struct and return the struct, but you cannot return the array.

    What your original code was doing was returning a single element of the array, namely element 9, and not an array. Here is a synopsis of the code you wrote:
    Code:
    double SomeFunction()
    {
        double SomeArray[9];
        //...
        return SomeArray[9];  // This does not return an array!
    }
    That returns a single value, SomeArray[9]. This is what your code was doing. Not only does it not return an array, it returns an item from an invalid index in the array -- there is no element 9, as the array is indexed from 0 to 8 (just as Philip pointed out).

    Also, please see what Philip pointed out -- you are accessing an invalid index in the array, causing the program to have undefined behaviour. When you access an invalid element in the array, the program may "work", may crash, or can change other values that you didn't suspect causing invalid values to be used.

    If you are a beginner in C++, it is imperative you know exactly what you're doing when you write a program. If you don't know about how to handle arrays, i.e. passing and returning from functions, then you need to study that and write simpler programs giving you practice in doing this, instead of trying to write full-blown C++ programs and not know the basics of C++.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    May 2012
    Posts
    8

    Re: How to return different array in different function?

    Also one thing you should consider is the maximum size of long double, assuming its 16 bytes then you get a maximum (signed) value of (2^128)-1, or 340282366920938463463374607431768211455. This is usually more than enough, but say if your array values exceed 10k each then you will overflow. You can get 2x the space by using unsigned long double, but you lose the ability to assign negative values

  7. #7
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: How to return different array in different function?

    Quote Originally Posted by ocm7 View Post
    Also one thing you should consider is the maximum size of long double, assuming its 16 bytes then you get a maximum (signed) value of (2^128)-1, or 340282366920938463463374607431768211455.
    No, long double is a floating point type; it's not an integer type.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  8. #8
    Join Date
    Jul 2012
    Posts
    7

    Re: How to return different array in different function?

    I know how can I do.
    Thank you very much~~

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured