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.