0

I have a problem to exchange the values of an index in a 2D array by using a pointer. My code for example has an two dimensional array with the initialization {{1,2},{2,3},{3,4},{4,5}} and I want to exchange the values of two indexes in the 2D array. I created a function named swap like this:

void swap(double points[20][2],int i, int j){
    double *a,*b;
    a= *(*(points +i)+ 0;
    *(*(points +i)+ 0 = points[j][0];
    *(*(points +j)+ 0 = a;
    b= *(*(points +i)+ 1;
    *(*(points +i)+ 1 = points[j][1];
    *(*(points +j)+ 1 = a;
} 

i,j here mean I want to change index i to j and vice-versa so for example when I used swap(points,0,1) the 2D array points will change like this: {{2,3},{1,2},{3,4},{4,5}} Any help would appreciated.

2 Answers 2

2

You can treat 2D array as a pointer to 1D arrays, and do a swap using a temporary array, like this:

void swap(double points[20][2],int i, int j) {
    double tmp[2];
    memcpy(tmp, points+i, sizeof(*points));
    memcpy(points+i, points+j, sizeof(*points));
    memcpy(points+j, tmp, sizeof(*points));
}

demo

The implementation is the classic swap, i.e.

tmp = points[i];
points[i] = points[j];
points[j] = tmp;

but since arrays in C cannot be assigned as a unit, memcpy is used instead.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your help. what is memcpy? memory copy right?
@Schrodinger Correct. More info on array assignment.
2

Here you are

void swap( double points[][2], size_t i, size_t j )
{
    double ( *a )[2] = points + i;
    double ( *b )[2] = points + j;

    for ( size_t i = 0; i < 2; i++ )
    {
        double tmp = ( *a )[i];
        ( *a )[i] = ( *b )[i];
        ( *b )[i] = tmp;
    }
} 

Or entirely without the subscript operator

void swap( double points[][2], size_t i, size_t j )
{
    double ( *a )[2] = points + i;
    double ( *b )[2] = points + j;

    for ( size_t i = 0; i < 2; i++ )
    {
        double tmp = *( *a + i );
        *( *a  + i ) = *( *b  + i );
        *( *b + i ) = tmp;
    }
} 

Here is a demonstrative program

#include <stdio.h>

#define N   2

void swap( double points[][N], size_t i, size_t j )
{
    double ( *a )[N] = points + i;
    double ( *b )[N] = points + j;

    for ( size_t i = 0; i < N; i++ )
    {
        double tmp = *( *a + i );
        *( *a  + i ) = *( *b  + i );
        *( *b + i ) = tmp;
    }
} 

int main(void) 
{
    double points[][N] =
    {
        { 1, 2 }, { 2, 3 }, { 3, 4 }, { 4, 5 }
    };

    const size_t M = sizeof( points ) / sizeof( *points );

    for ( size_t i = 0; i < M; i++ )
    {
        printf( "{ %.1f, %.1f } ", points[i][0], points[i][1] );
    }

    putchar( '\n' );

    swap( points, 0, 1 );

    for ( size_t i = 0; i < M; i++ )
    {
        printf( "{ %.1f, %.1f } ", points[i][0], points[i][1] );
    }

    putchar( '\n' );

    return 0;
}

The program output is

{ 1.0, 2.0 } { 2.0, 3.0 } { 3.0, 4.0 } { 4.0, 5.0 } 
{ 2.0, 3.0 } { 1.0, 2.0 } { 3.0, 4.0 } { 4.0, 5.0 } 

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.