I think you see the arr is not initialized:
- alloc memory?
- default values.
- float arr[][] is not same with float **arr, you should use like this:
float (*A)[2]
But in your main function, you have finished the alloc work. The arr is allocated at the stack.
So in your print function, what you have to do is only print the result or initialize each item's value.
#include <stdio.h>
#include <stdlib.h>
void printArr(float (*A)[2],int w, int h){
int i,j;
//Wrong, A has been alloced.
/*
A = (float*) malloc(w*h*sizeof(float));
for (i=0;i<h;i++){
A[i]=(float*) malloc(w*sizeof(float));
}
*/
for (i=0;i<h;i++){
for(j=0;j<w;j++){
printf("%f ",A[i][j]);
}
printf("\n");
}
}
void printPointer(float *A,int w, int h){
int i,j;
//Wrong, A has been alloced.
/*
A = (float*) malloc(w*h*sizeof(float));
for (i=0;i<h;i++){
A[i]=(float*) malloc(w*sizeof(float));
}
*/
for (i=0;i<h;i++){
for(j=0;j<w;j++){
printf("%f ",*((A+i*h)+j));
}
printf("\n");
}
}
int main(void) {
int x_dimension=3;
int y_dimension=2;
//By Array, Array is not a pointer, but is a structure
float arr[2][3] = {};
//Only [0][0] item has been initialized
arr[0][0]=16.2f;
printArr(arr,x_dimension,y_dimension);
//By pointer
float* arrp = (float*)calloc(x_dimension*y_dimension,sizeof(float));
*arrp=16.2f;
printPointer(arrp,x_dimension,y_dimension);
return 0;
}