Passing array by reference

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • news.tiscali.dk

    Passing array by reference

    This might be a really simple question, but here goes: How can I pass an
    array to a function and have the function put values into that array? Some
    code to clearify (mat is a matrix class)

    float m[16];
    mat = matX * matZ * matT;
    mat.getElements ( m );
    Render.setMeshT ransform( m );

    In my matrix class I have

    void getElements( TYPE tMatrix[] ) {
    tMatrix = m_tElements;
    }

    float m_tElements[16];

    This, of course, works one way; getElements can access the values in
    tMatrix. But when the function returns, m is not filled with the values from
    m_tElements. So, is it possible to somehow pass an array by reference,
    (without new'ing it!)?




  • Ron Natalie

    #2
    Re: Passing array by reference


    "news.tiscali.d k" <bush@whitehous e.gov> wrote in message news:DO1cb.8994 9$Kb2.3744735@n ews010.worldonl ine.dk...[color=blue]
    > This might be a really simple question, but here goes: How can I pass an
    > array to a function and have the function put values into that array? Some
    > code to clearify (mat is a matrix class)[/color]


    Arrays are always passed by reference (this is a goofy inconsistency that's
    been in C since the dawn of time). Well, what really happens is that the thing
    that looks like an array declaration in your getElements function really gets
    converted to a TYPE* tMatrix. So you're passing a pointer to the first float
    in the array. This isn't your problem.

    The real problem you are running into, is that arrays are also inconsistant
    in that they can't be assigned.
    [color=blue]
    > void getElements( TYPE tMatrix[] ) {
    > tMatrix = m_tElements;
    > }[/color]

    You'll have to copy the elements individually:
    for(int i = 0; i < 16; ++i) tMatrix[i] = m_tElements[i];
    for example.

    However, what's really easier is to use vector. vector is an array like
    object that behaves normally (unlike arrays). If you want to pass it by
    reference, you can use the C++ reference syntax.

    #include <vector>
    using std::vector;

    vector<float> m(16);
    mat.GetElements (m);

    vector<float> m_tElements(16) ;
    void getElements(vec tor<TYPE>& tmatrix) {
    tmatrix = m_tElements;
    }



    Comment

    • John Almer

      #3
      Re: Passing array by reference

      > However, what's really easier is to use vector. vector is an array like[color=blue]
      > object that behaves normally (unlike arrays). If you want to pass it by
      > reference, you can use the C++ reference syntax.
      >
      > #include <vector>
      > using std::vector;
      >
      > vector<float> m(16);
      > mat.GetElements (m);
      >
      > vector<float> m_tElements(16) ;
      > void getElements(vec tor<TYPE>& tmatrix) {
      > tmatrix = m_tElements;
      > }[/color]

      Op should also check out "vector::swap() " which many programmers don't seem
      to be aware of (not referring to you personally :). It's much faster than
      direct assignment where applicable.


      Comment

      Working...