I'm trying to find a way to find the size of 2D array. Here is the function I use to create a matrix:
float** createMatrix(int rows, int colums,char populate){
float** matrix = malloc(sizeof(float*)*rows); // allocates the memory for all the pointers of the 2D array
for(int i = 0;i<rows;i++){ // allocates the memory for each pointer in the 2D array
matrix[i] = malloc(sizeof(float)*colums); // allocates the memory for all the colums in each row
}
if(populate=='Y'){
for(int i = 0;i<rows;i++){ // prompts the user for values to create the array
for(int j = 0;j<colums;j++){
printf("Value for row %d colum %d: ",i+1,j+1);
scanf("%f",&matrix[i][j]);
}
}
}else if(populate=='N'){
return matrix;
}
return matrix; // returns the matrix created
}
Testing it out:
float** matrix = createMatrix(100,100,'N');
int rows = sizeof(matrix)/sizeof(float*);
int colums = sizeof(matrix[0])/sizeof(float) - 1;
printf("Rows: %d and Cols: %d",rows,colums);
I get "Rows: 1 and Cols: 1" as the output. I'm not sure what I'm doing wrong?