0

hello i want to assing return array from method to an array. My codes here. how can assing array to array like this?

method change codes:

int* change (int array[], int index1, int index2) {

    int temp;

    temp = array[index1];
    array[index1] = array[index2];
    array[index2] = temp;

iter++;

    return array;
}

method combine's codes here:

static void combine(int mat[], int len) {

    if(ok)
    return;

    int array[len];
    *array = *mat;


    if (len <= sat * sut) {

        for (int i = len; i < sat * sut - 1; i++) {

            for (int j = i; j < sat * sut; j++) {

             // this is error row
             combine(array, len + 1);

           array = change(array, i, j);

                if (isAcceptable(array) == "ACCEPTABLE") {

                    int accepted[sat*sut];
                    *accepted = *array;
                    ok = true;
                    return;
                }



            }
        }
    } else
        return;
 }

error row is here:

array = change(array, i, j);

error: [Error] incompatible types in assignment of 'int*' to 'int [(((sizetype)(((ssizetype)len) + -1)) + 1)]'

how can i fix it?

1
  • An aside, but you might like to use std::swap in your change function. Commented Dec 11, 2014 at 17:41

1 Answer 1

1
array = change(array, i, j);

As the error message says, you're trying to assign a pointer to an array, which makes no sense. There's no need for that; just call the function to change the array:

change(array, i, j);

and change the function to return void - there's no point simply passing back one of the arguments, since the caller already knows what is was. Or don't bother writing a function at all:

std::swap(array[i], array[j]);

Also, this looks a bit dodgy:

*array = *mat;

Perhaps you think this copies the entire array into array; it doesn't, it only copies the first element. To copy the array, do

std::copy(mat, mat+len, array);

or better still, use a standard container rather than a non-standard variable-length array:

std::vector<int> array(mat, mat+len);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.