error: invalid conversion from ‘int*’ to ‘int’ [-fpermissive]

at compile time i am getting this:
 
error: invalid conversion from ‘int*’ to ‘int’ [-fpermissive]


the function with the offending line:

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
int
decode_hamming(int encoded_lcw[])
{
    int edata[10];
    for (int index = 0; index < 24; index++)
    {
        //  get 24 10-bit codewords out of 240 bit aencoded_lcw array
        //  and put the 10 bit arrays in edata[]
        memcpy(edata, (encoded_lcw+10*index), 10);
        
        int encoded[11],syndrome[4];
        int hmatrix[4][10] =	//parity matrix
                        {
    					1, 1, 1, 0, 0, 1, 1, 0, 0, 0,
                        1, 1, 0, 1, 0, 1, 0, 1, 0, 0,
                        1, 0, 1, 1, 1, 0, 0, 0, 1, 0,
                        0, 1, 1, 1, 1, 0, 0, 0, 0, 1
    					};
        
        int i;
        int j;
    	for(i=0;i<4;i++)
    	{
    		for(j=0;j<10;j++)
    		syndrome[i]+=(edata[j]*hmatrix[i][j]);
    		syndrome[i]=syndrome[i]%2;
    	}
    	for(j=0;j<10;j++)
    	if((syndrome[0]==hmatrix[0][j]) && (syndrome[1]==hmatrix[1][j])&& (syndrome[2]==hmatrix[2][j]) && (syndrome[3]==hmatrix[3][j]))
    	break;
    	if(j==10)
    	printf("\nError free\n");
    	else
    	{
    		//printf("\nError recieved at bit number %d of data\n",j+1);
    		edata[j]=!edata[j];
    		//printf("\nCorrect data should be : ");
    		for(i=0;i<10;i++)
    			printf("%d",edata[i]);
                //edata[i] is the corrected ten bit codeword
                //have to remove the last 4 parity bits to get the original 6 bit data or hex bits
                //then put the 24 hex bits into an array for the reed-solomon decoder below
    	}
         // put all 24 edata[] 6-bit arrays into one array 
        memcpy((rs_codewords+index*6), edata, 6);
        
    }
    return rs_codewords;  //should be 144 bit array for the rs decode
}


the return statement in the second to last line is the offending line. i have googled and been looking at my text book all day. i am using g++ on ubuntu. any guidance will be much appreciated. Thanks!
I'm guessing rs_codewords is some kind of array, and is not an int.

Your function is indicated to return an int.
1
2
3
4
int decode_hamming(int encoded_lcw[])
 ^
 |
 The return type


Note it's int, singular. Not an array of ints, but a single int.

You cannot return an array from a function. What you can do is return a pointer to the array (which only works if the array is not local to the function)... or you can pass the array to the function as a parameter by pointer and modify its contents that way.


But to really know what the best solution here is.... where/how is 'rs_codewords' declared?
right; rs_codewords is declared at the top of the file, along with other two arrays, like this:

1
2
3
4
5
int encoded_lcw[240];
int rs_codewords[144];
int decoded_lcw[72];
int decode_hamming(int encoded_lcw[]);
int decode_reed_solomon(int rs_codewords[]);


we cannot return an array from a function; right. so i have to use pointers. before i tried my latest nonsense, i have been unable to get the syntax right for pointers copying strait out of my text book. stuff like:

1
2
3
int array[10];
typedef int* IntPtr;
IntPtr a;


1
2
int * array;
array = new int [5];


or:
 
int * rs_codewords = new int[144]; 


simply would not compile without mega complaints. what i am trying to is get array 1 into the above posted function, do some work on that array and put the results into array 2, reference array 2 into another function, do some work on it and return a pointer to that array 3.

I have been unable to come up with the right syntax. THanks!
Any time the topic comes to pointers.... I feel like I have to explain the nuances of memory ownership. Because unless you understand it, pointers are very easy to get wrong.

Returning a pointer from a function is generally a bad idea because the pointer has to point to memory which is owned by something larger than the function. And if the function doesn't own the memory, then it probably shouldn't be returning a pointer to it.

In a case like this... my first instinct would be to return void... and instead of having a global rs_codewords array (globals are evil!!! avoid! avoid!) pass the array to the function as a parameter.

So this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

// get rid of global rs_codewords


void decode_hamming(int encoded_lcw[], int rs_codewords[])
{
    // modify rs_codewords elements here and they will be visible outside the function
    //  since the array is being passed by pointer

    // no need to return anything, as the function now returns void
}

int main()
{
    int rs_codewords[144];
    int encoded_lcw[ ... ];

    ...

    decode_hamming( encoded_lcw, rs_codewords );
    // the above call will modify rs_codewords.
    //   you can see it's changes in main now
}
ok so i did what you said and it compiles without complaint. thank you. here is my 'main' function that will be called in another file:

1
2
3
4
5
6
7
8
9
10
int lcw_ecc()
{
    int encoded_lcw[240];
    int rs_codewords[144];
    int decoded_lcw[72];
    void decode_hamming(int encoded_lcw[], int rs_codewords[]);
    void decode_reed_solomon(int rs_codewords[], int decoded_lcw[]);
    decode_hamming( encoded_lcw, rs_codewords );
    decode_reed_solomon( rs_codewords, decoded_lcw );
}


now that i have this function and it compiles error free how shall i call it in the function in the other file that i will include this in??? I was thinking like this:
 
int decoded_lcw[72] = lcw_ecc(encoded_lcw, 240);

but if i put the 'encoded_lcw[]' as a parameter
 
int lcw_ecc(int& encoded_lcw[]) 

the compiler rejects it:
 
error: declaration of ‘encoded_lcw’ as array of references

or without ampersand:
 
error: declaration of ‘int encoded_lcw [240]’ shadows a parameter

I am afraid Ive gotten myself confused here about how to call by reference. Thanks for everything.



again... low_ecc is set to return a single int. This is not what you want that function to do. You are not outputting a single int and therefore that is not the correct return type. You want to output an array of ints.

When an array is your function's output, you do not return it. Remember that you cannot really return arrays. So instead... you return void and have the function output to a parameter by pointer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  void func(int* foo) //  a function that takes an array by pointer
//void func(int foo[]) // alternative, but more misleading way to declare the exact same thing
{
   foo[0] = 10;
   foo[1] = 16;
   foo[2] = 5;
}

int main()
{
   int x[3] = {1,2,3};  // an array of 3 elements

   func( x ); // pass our array to the function as a pointer.
      // C++ will automatically cast the array to be a pointer to the first element

   cout << x[0];  // prints "10"
   cout << x[1];  // prints "16"
   cout << x[2];  // prints "5"
}
Last edited on
right, so i did this to the function definition:
1
2
3
4
5
6
7
8
9
10
void lcw_ecc(int encoded_lcw[], int decoded_lcw[])
{
    //int encoded_lcw[240] ;
    int rs_codewords[144] ;
    //int decoded_lcw[72] ;
    void decode_hamming(int encoded_lcw[], int rs_codewords[]);
    void decode_reed_solomon(int rs_codewords[], int decoded_lcw[]);
    decode_hamming( encoded_lcw, rs_codewords );
    decode_reed_solomon( rs_codewords, decoded_lcw );
}

and it compiles now. thanks.

and i thought id call it in my other function definition like this:
1
2
3
4
5
6
7
8
9
10
11
// function definition that extracts bits from a queue
uint16_t ldu1::decode_lcw() const  
{
   . . .
   . . . 
   static uint16_t encoded_lcw = LCW_BITS;

   //call the error correction code from lcw_ecc.h 
   int decoded_lcw[72];   
   lcw_ecc(lcw, decoded_lcw);
}

so would this call to lce_ecc() work?
would the ecoded_lcw' array will be available for inspection in the .cc file where it the lcw_ecc() is called?

Last edited on
You don't show the type of "lcw".

Available? Yes. The name "decoded_lcw" within function lcw_ecc (when called by ldu1::decode_lcw) does refer to the same memory block (of 72 integers) as name "decoded_lcw" in the function ldu1::decode_lcw.
im sorry line 10 in the above code should have been:
 
lcw_ecc( encoded_lcw, decoded_lcw);


what if i declare the "decoded_lcw[]" under 'public' or 'protected' in the header file for ldu1? will that make it available for inspection outside of the ldu1::decode_lcw() ??

another question i have is with all of these pointers flying around should i deallocate the memory the arrays use at some point? or at least setting the arrays to 0? lcw_ecc will be called about 6 times per second will the arrays have left over values in them or will they simply overwrite or???
Last edited on
Topic archived. No new replies allowed.