to dynamically allocate a 2-dimensional array, use something like this:
int** array = malloc(sizeof(int*)*ARRAYSIZE);
Here you allocate an array of pointers to int, now you must allocate memory for each pointer:
for(int i = 0;i<ARRAYSIZE;i++) array[i] = malloc(sizeof(int)*INNER_ARRAYSIZE);
And now fill each record with your actual data:
for(int i = 0;i<ARRAYSIZE;i++) for(int j = 0;j<INNER_ARRAYSIZE;j++) array[i][j]=(i+j);
And update the ThreadData structure to use two-dimensional arrays:
struct ThreadData { int start, stop; int** twoDimArray;
};
And just pass the pointer here:
struct ThreadData data; data.twoDimArray = array; data.twoDimArray[0][0] = data.twoDimArray[0][0]*data.twoDimArray[0][0]; //access element at 0,0 and square it
source share