I've written the following generic function:
void* scramble(void* arr, int ElemSize, int n, int* indArr){
void * res = malloc(n * ElemSize);
int i;
for (i = 0 ; i < n ; i++)
memcpy((BYTE*)res+i*ElemSize, (BYTE*)arr+indArr[i]*ElemSize, ElemSize);
return res;
}
The function gets an array, the size of each element, and number of elements in the array.
indArr is an array with indexes running from 0 to n-1 in any order.
The function returns a new array arranged according to indArr.
How can I use it to work with two dimensional arrays (or more)? I tried this, but it doesn't work:
int indArr[] = {2, 0, 1};
char names[][10] = {{"David"}, {"Daniel"}, {"Joni"}};
char **res;
res = (char**)scramble(names, sizeof(char*), 3, indArr);
for (i = 0 ; i < 3 ; i++)
printf("%s ", res[i]);
Any thoughts? Thanks