Arrays: passing by value, reference, and/or pointer ?!?

I have gotten myself thoroughly confused about passing by value, reference, and pointer. And how to make an array available for inspection outside of the function where it (or where a pointer pointing to it ??) is first used as a parameter.

the first function i have fills an array with the values from certain positions in a data packet.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// code in  a .cc file
void 
ldu1::decode_lcw()
{
   uint16_t LCW_BITS[] = 
   {
      410,  411,  412,  413, . . . . . . . . .....
   };
   uint16_t LCW_BITS_SZ = sizeof(LCW_BITS) / sizeof(LCW_BITS[0]);
   uint16_t LCW_BITS = extract(frame_body(), LCW_BITS, LCW_BITS_SZ);
   uint16_t encoded_lcw = LCW_BITS;

   //call the error correction code from another file, lcw_ecc.h 
   //  decoded_lcw is declared protected in 
   //  the .h file corresponding to this .cc file
   lcw_ecc(encoded_lcw, decoded_lcw);
}

line 16 above calls the 'main' function from an included file that is to do the error correction math on the bits in "encoded_lcw". below is that function definition:
1
2
3
4
5
6
7
8
9
10
//  code in another file, lcw_ecc.h, which holds the  
//  error correction function definitions
void lcw_ecc(uint16_t encoded_lcw[], uint16_t decoded_lcw[])
{
    uint16_t rs_codewords[144] = {0} ;
    void decode_hamming( uint16_t encoded_lcw[], uint16_t rs_codewords[] );
    void decode_reed_solomon( uint16_t rs_codewords[], uint16_t decoded_lcw[] );
    decode_hamming( encoded_lcw, rs_codewords );
    decode_reed_solomon( rs_codewords, decoded_lcw );
}

the lcw_ecc() is to pass encoded_lcw to the decode_hamming(). the decode_hamming() takes the values out of the encoded_lcw array, do some math shedding 96 bits, and put the decoded 144 bits into a new array "rs_codewords". the decode_reed_solomon() is to take the values from rs_codewords[], do some math and shedding 72 bits yeilding the values that are put into the final 72 bit array "decoded_lcw".

My question: is this an appropriate way to pass things (arrays or pointers to those arrays) around?? the two decode functions work in terminal as singletons, and the whole thing compiles without error when i put the 3 files into the library dir, but i have no confidence that i have done this right (connecting everything) and I am having a helluva time trying to figure out the right way to make the "decoded_lcw" available for inspection in the file outside of the "ldu1::decode_lcw()".

Arrays always pass by reference.
so should i be using & in this syntax? or is the syntax i have OK? Will what i have make the "decoded_lcw" available in the file outside of the ldu1::decode_lcw() ? Thanks!
Arrays are always passed by reference, so when you send an array to a function or method, that function or method will be able to modify that array. Using the '&' will actually give you an error (if I remember correctly) and a pointer is unnecessary (the address is already being passed).

Just be careful of modifying the array when you don't want to.
right well only the first array is not to be changed. the other 2 are changed, about 6 times a second. they are initialized to 0 and then given their values on every call.

i apologize if this a crazy rookie question but i cant proceed without being sure: will declaring the "decoded_lcw" protected in the .h file make it available outside of the "ldu1::decode_lcw()"? or is there some other way that is more appropriate to accomplish this??

Thanks!!
Last edited on
I never allow my variables in a class to be public (I don't trust other classes). If you want to make it available to other classes, you could make it public or private with an accessor method and some setter method.

I program primarily in Java (derived from c++) so I don't know exactly how to do what you're asking, but this is my two cents.
thanks a bunch for you help;

decoded_lcw first appears in "ldu1::decode_lcw()" so that function "owns" the memory used by decoded_lcw. right? the files i have mentioned are the only two files this array should be available so i know i cant make it public. i am unsure but i think if i make it private it will not be available outside of the function the array is first used?? or am i wrong about this?

so here is a correction to my question:
so will declaring the "decoded_lcw" 'protected', in the .h corresponding to the .cc file where the array first makes its appearance; will declaring it like this, make the modified array available, not only to "ldu1::decode_lcw() where it is first used in a parameter, but also available to the whole .cc file where this function is?? because i need to read the data payload using other functions contained in the same file.



Pointers to the beginning of each array is passed by value to:
void lcw_ecc(uint16_t encoded_lcw[], uint16_t decoded_lcw[])

Perhaps, you could do something like this:
(Just ignore the Java white noise; in C++, we can, and we do, express the notion of immutability).

In the header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct ldu1
{
    // specify the size of the two arrays (put in the actual sizes)
    enum { ENCODED_LCW_SIZE = ...,  DECODED_LCW_SIZE = ... };

    private:
        // declare the arrays as private static members
        static uint16_t encoded_lcw[] ;
        static uint16_t decoded_lcw[] ;

    public:
        // provide public static inspectors for the two arrays
        static const uint16_t*  encoded_array() { return encoded_lcw ; }
        static const uint16_t*  decoded_array() { return decoded_lcw ; }

        static void decode_lcw() ; // and the function in question
};


In the cpp file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// define encoded_lcw, decoded_lcw
uint16_t ldu1::encoded_lcw[ ldu1::ENCODED_LCW_SIZE ] ;
uint16_t ldu1::decoded_lcw[ ldu1::DECODED_LCW_SIZE ] ;


void ldu1::decode_lcw()
{
    // assign values to encoded_lcw
    for( int i = 0 ; i < ENCODED_LCW_SIZE ; ++i )
    {
        encoded_lcw[i] = 0 ;
    }

    // call void lcw_ecc() passing pointers to the beginning of the two arrays 
    // it puts the result in decoded_lcw
    lcw_ecc(encoded_lcw, decoded_lcw) ;
}


And, where you want to access (inspect, but not modify) the arrays:

1
2
3
4
5
6
7
8
9
10
11
12
void foo()
{
    // inspect the decoded_array
    
    const uint16_t* decoded_array = ldu1::decoded_array() ;
    for( int i = 0 ; i < ldu1::DECODED_LCW_SIZE ; ++i )
    {
        // inspect each element of the array
        uint16_t value = decoded_array[i] ;
        // ...
    }
}
Sorry I couldn't help too much. I haven't used C++ and classes for a while because I'm taking Java courses. On top of that, today just isn't my day. I looked at the title and thought that I could help by telling you that arrays are passed by reference. It got a lot more complicated when you started asking other questions.
no probs man. one does what one can.
Topic archived. No new replies allowed.