Don't de-reference centroid when passing in. Doing that will only pass your first row. Do this:
initialize_centroids( centroid, noofcentroids, newnoofvar );
Then you need to use the correct type here:
void initialize_centroids( int **carray, int p, int q )
Your array is presumably an int**. That means it's a pointer to a location in memory that contains an array of int* pointers. Each of those pointers (I assume) references memory that has been allocated one row of your array.
Now, once you obtain one of these pointers by doing carray[i] you have selected row i. You now have an int* which means it points to an array of int.
Finally, carray[i][j] selects a specific integer from column j on row i.
[edit]
Theory about what might be going wrong... You may not be allocating a 2D array in a dynamic sense, or indeed it might be a 1D array and you are supposed to use stride-lengths etc. Here is a solution that you can use for allocating a 2D array dynamically, and indexing it as carray[i][j].
Note: Some people have gotten upset at me for this method in the past due to alignment concerns, but I have never had issues. I think they were just having a grumpy day. For those who are worried about alignment, it's easy to adjust for that, but is too complicated for this example.
int** allocate_array2d( int rows, int cols )
{
int i, **arr, *data;
arr = (int**)malloc( rows * sizeof(int*) + rows*cols*sizeof(int) );
data = (int*)(arr + rows);
for( i = 0; i < rows; i++ ) {
rows[i] = data;
data += cols;
}
return arr;
}
void free_array2d( int** arr ) {
free((void*)arr);
}
Now, in your main:
int** centroid = allocate_array2d( noofcentroids, newnoofvar );
initialize_centroids( centroid, noofcentroids, newnoofvar );
[more...]
Okay.Yes I allocated memory like this centroid[100][100] – Bobby 11
mins ago
In that case, you should declare:
void initialize_centroids( int carray[100][100], int p, int q )
{
//...
}
No need to use my dynamic array example
int *is not in any way "a double-dimensional array".