how to pass pointers

I am sure this is a super rookie problem i am having but I have been stuck for a very long time, and i cant find a simple explanation on the web or in my textbook. I am trying to add some functions to a class in a larger project. The class i am working, is the last derived class, and has the two files, the .h and .cc file. I have all my functions declared in the .h file and the definitions are in the .cc file. i need to use 3 arrays that need to be modified (or filled) by functions and then passed to other functions to be read and copied in various ways. My problem is that, in order to maintain the style of what has already been implemented, I have been told that i do not need, and should not, declare variables in the .h file simply to use with member functions; that simply passing appropriate parameters is all that is needed. i have been unable to do this successfully.

here is my first function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  //take 240 bits our of frame_body()
//decode them and return pointer to decoded_lcw[]
uint16_t
ldu1::decoded_lcw()
{
   const size_t LCW_BITS[240] = {
      410,  411,  412, . . . positions to extract values from . . .
     
   };
   const uint16_t LCW_BITS_SZ = sizeof(LCW_BITS) / sizeof(LCW_BITS[0]);
   // assign values to encoded_lcw
   uint16_t * new = encoded_lcw[240];
   for( int i = 0 ; i < LCW_BITS_SZ; ++i ){
        encoded_lcw[i] = extract(frame_body(), LCW_BITS[i], LCW_BITS_SZ);
   }
   uint16_t * new = rs_codewords[144];
   // should take 240 bit encoded_lcw and returns rs_codewords
   rs_codewords = decode_hamming( encoded_lcw, rs_codewords );
   uint16_t * new = decoded_lcw[72];
   // should take 144 bit rs_codewords and return decoded_lcw
   decoded_lcw = decode_reed_solomon( rs_codewords, decoded_lcw );
   return decoded_lcw;  
}

errors from above function:
1
2
3
4
5
6
7
8
9
10
ldu1.cc: In member function 'uint16_t ldu1::decode_lcw()':
ldu1.cc:76:15: error: expected unqualified-id before 'new'
ldu1.cc:76:15: error: expected initializer before 'new'
ldu1.cc:80:15: error: expected unqualified-id before 'new'
ldu1.cc:80:15: error: expected initializer before 'new'
ldu1.cc:81:61: error: incompatible types in assignment of 'void' to 'uint16_t [144] {aka short unsigned int [144]}'
ldu1.cc:82:15: error: expected unqualified-id before 'new'
ldu1.cc:82:15: error: expected initializer before 'new'
ldu1.cc:83:65: error: incompatible types in assignment of 'void' to 'uint16_t [72] {aka short unsigned int [72]}'
ldu1.cc:84:11: error: invalid conversion from 'uint16_t* {aka short unsigned int*}' to 'uint16_t {aka short unsigned int}' [-fpermissive]


the decode_hamming function that is called above:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//  supposed to take in 240 bit encoded_lcw array
//  and return 144 bit array rs_codewords
uint16_t 
decode_hamming( uint16_t encoded_lcw[], uint16_t rs_codewords[] )
{
    
    for (int index = 0; index < 24; index++)
    { 
       // bunch of numerical methods doing algebric decoding
       // . . . blah blah blah
       //  put 24 6-bit arrays into one array, rs_codewords
       memcpy((rs_codewords+index*6), edata, 6);
    }
    return rs_codewords;
}

error associated with this function which is the same for the reed solomon function:
1
2
ldu1.cc: In function 'uint16_t decode_hamming(uint16_t*, uint16_t*)':
ldu1.cc:224:13: error: invalid conversion from 'uint16_t* {aka short unsigned int*}' to 'uint16_t {aka short unsigned int}' [-fpermissive]


and how i want to use the decoded_lcw array:
1
2
3
4
5
6
7
8
9
10
11
// read the bits of the decoded_lcw array   
string
ldu1::payload_field(uint16_t decoded_lcw[])
{
   // here should be some code to take the bits out of specified postions
   // in decoded_lcw[], convert bits to hex, and return as string 
   const uint16_t payload_field = // some code i dont have yet
   ostringstream os;
   os << hex << showbase << setfill('0') << setw(6) << payload_field;
   return os.str();
}

i have to extract five data payload fields from the decoded_lcw.

What seems so hard for me to find an answer to is: (1) how and where (if not in the header file) exactly do i declare the arrays and/or pointers??? ; and (2) exactly how do i pass the pointers around in the parameters to make the arrays available to the functions that need to read them?

please, need help. any guidance will be much appreciated.

Last edited on
Your syntax is wrong: uint16_t * new = encoded_lcw[240];

Should be: uint16_t * encoded_lcw = new uint16_t[240];

Here's a simple example of passing pointers using your variable and function names:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <stdint.h>

void decode_hamming(uint16_t *encoded_lcw, uint16_t *rs_codewords){
    // do your processing here  
    // using pointers will change rs_codewords in the calling function
    for(uint16_t i=0; i<2; ++i)
        rs_codewords[i] = encoded_lcw[i];
    rs_codewords[1] = 99; // made change to show that passing pointers works
}

void decode_reed_solomon( uint16_t *rs_codewords, uint16_t *decoded_lcw ){
    for(int i=0; i<2; ++i)
        decoded_lcw[i] = rs_codewords[i];
    decoded_lcw[0] = 9;
}

void decode_lcw(uint16_t *decoded_lcw){
    uint16_t *encoded_lcw = new uint16_t[2];
    encoded_lcw[0] = 0;
    encoded_lcw[1] = 0;
    uint16_t *rs_codewords = new uint16_t[2];
    decode_hamming(encoded_lcw, rs_codewords);
    delete [] encoded_lcw; // don't leak memory 
    decode_reed_solomon( rs_codewords, decoded_lcw );
    delete [] rs_codewords; 
}

int main()
{
    uint16_t decoded_lcw[2] = { 42, 42 };
    decode_lcw(decoded_lcw);
    std::cout << decoded_lcw[0] << "\t" <<decoded_lcw[1] << std::endl;
    return 0;
}


HTH
Last edited on
thanks a bunch! i did what you put here and now it compiles without error. you did here what is nowhere else could do ;-)

i do have a couple new questions now: (1) we do not need to declare the decoded_lcw[72] ?? (2)do we need to have a 'delete [] decoded_lcw;' ??.

Thanks!
Last edited on
Topic archived. No new replies allowed.