JMzance
JMzance

Reputation: 1746

Passing 2D arrays to a function

I'm trying to pass two 2D arrays (both the same size; 8 by 4) to a function and set one equal to the other (with some elements in a different order but that doesnt really matter). So far I have:

int main() {
    double** Array1;
    double** Array1;

    // MALLOC BOTH OF THEM
    ....

    // PUT STUFF IN ARRAY1
    ....

    CopyFunction(&Array1, &Array2);
}

void CopyFunction(double*** Array1, double*** Array2) {
    for (int i = 0; i < 8; i++) {
        *Array2[i][0] = *Array1[i][0];
        *Array2[i][1] = *Array1[i][1];
        *Array2[i][2] = *Array1[i][2];
        *Array2[i][3] = *Array1[i][3];
    }
}

But I am getting segfault errors like this:

 *** Break *** segmentation violation


===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================

Upvotes: 0

Views: 108

Answers (4)

Cornstalks
Cornstalks

Reputation: 38218

You want:

    (*Array2)[i][0] = (*Array1)[i][0];
    (*Array2)[i][1] = (*Array1)[i][1];
    (*Array2)[i][2] = (*Array1)[i][2];
    (*Array2)[i][3] = (*Array1)[i][3];

As it's written, you're not dereferencing the arrays in the right order, due to order of operations. The dereferences done by [i][N] are happening before *. The original version is the same as doing *(*(*(Array1 + i) + N)) (due to how [] really works), but what you want is something like *(*(*Array1 + i) + N), which is why you have to add parentheses like I did.

Even better, though, would be this:

void CopyFunction(double** Array1, double** Array2) {
    for (int i = 0; i < 8; i++) {
        Array2[i][0] = Array1[i][0];
        Array2[i][1] = Array1[i][1];
        Array2[i][2] = Array1[i][2];
        Array2[i][3] = Array1[i][3];
    }
}

And even better than that would be this:

std::copy_n(*Array1, 32, *Array2);

Edit: though std::copy_n doesn't really work for you if you're changing the order of some of the elements when you do the copy.

Upvotes: 5

Hrishi
Hrishi

Reputation: 7138

int main() {
double** Array1;
double** Array1;

// MALLOC BOTH OF THEM
....

// PUT STUFF IN ARRAY1
....

CopyFunction(Array1, Array2);

}

void CopyFunction(double** Array1, double** Array2) {
   for (int i = 0; i < 8; i++) {
    Array2[i][0] = Array1[i][0];
    Array2[i][1] = Array1[i][1];
    Array2[i][2] = Array1[i][2];
    Array2[i][3] = Array1[i][3];
}

}

You should probably consider passing in the array sizes as a parameter.

Upvotes: 1

mshriv
mshriv

Reputation: 322

You should be doing

(*Array2)[i][0] = (*Array1)[i][0];

instead of

*Array2[i][0] = *Array1[i][0];

As index operator has higher precedence

Upvotes: 3

Nemanja Boric
Nemanja Boric

Reputation: 22157

Array subscript has a higher precedence than * operator, so you need to include parentheses in order to first dereference Array2 pointer to get a 2D array:

    (*Array2)[i][0] = (*Array1)[i][0];

http://en.cppreference.com/w/cpp/language/operator_precedence

Upvotes: 2

Related Questions