Kortatu Kortatu - 25 days ago 6
C++ Question

Vector return from a function

I'm trying to give an array as a pointer to a function as I want to return an array from it. The problem comes when calling to the function. I have the following code:

void function(unsigned char p[4][4], unsigned char x, unsigned char* arr[4]) {
unsigned char result;
for (int ii=0;ii<4;ii++)
{
result= p[ii][1] - p[ii][2] + p[ii][0]*x + p[ii][3]*4;
*arr[ii]=result;
}
unsigned char function2(unsigned char p[4][4], unsigned char x, unsigned char y) {
unsigned char arr[4];

function(p, y, arr);
ret= function3(arr, x);
return ret;
}


The objective is to modify the value of the vector
arr
in
function
, in order to use it in
function3
, that has nothing to do with
function
. The compilator is saying that:


cannot convert 'unsigned char*' to 'unsigned char**' for argument '3'
to 'void function(unsigned char (*)[4], unsigned char, unsigned
char**)


Any help is appreciated!

Answer

I think you can simple pass array as pointer (without pointer to array):

void function(unsigned char p[4][4], unsigned char x, unsigned char * arr) {
    unsigned char result;
    for (int ii = 0; ii < 4; ii++)
    {
        result = p[ii][1] - p[ii][2] + p[ii][0] * x + p[ii][3] * 4;
        arr[ii] = result;
    }
}

I think it is easiest way.