Home > Enterprise >  How do I pass and manipulate the arguments of a matrix into the function using pointers?
How do I pass and manipulate the arguments of a matrix into the function using pointers?

Time:01-28

i have managed to send the arguments using pointers and matrix size inside of main like so:

int main()
{

    swap(*(matrix 0) 1, 0, 1, *(matrix 3) 2, 3, 2);
}

my question really is, how do I swap out these two values of the matrix using the following function?

this is what I have so far in regards to the function:

void swap(float **a, int i, int j, float **b, int x, int y)
{

    float temp;
    temp = *(a i) j;
    *(a i) j = *(b x) y;
    *(b x) y = temp;
}

The errors are: incomaptible types when assigning to type 'float' from type 'float *', and lvalue required as left operand of assignment.

CodePudding user response:

temp = *(a i) j;

The left-hand side here is still a pointer-to-a-float. You have not fully dereferenced your double-pointer parameter a. It's the same as writing:

temp = a[i] j

what you probably meant was:

temp = a[i][j]

but actually, you could write it this way:

void swap_floats(float* a, float* b)
{
    float temp = *a;
    *a = *b;
    *b = temp;
}

void swap_matrix_elements(
    float** matrix_a, int row_a, int col_a,
    float** matrix_b, int row_b, int col_b)
{
    swap_floats( &(a[row_a][col_a]), &(b[row_b][col_b]) );
}
  •  Tags:  
  • Related