I am very new to use C
, I wrote the code bellow to create a 2d int
array, can anyone help to generic it to other types, like float, double ... ? or other function can be use directly?
int** alloc_arrays(int m,int n) {
int **MN = (int **)malloc(sizeof(int *)*m);
if(MN == NULL) {
exit(1);
}
for(int i=0; i<m;i++) {
MN[i] = (int *)malloc(sizeof(int)* n);
if(MN[i] == NULL){
exit(1);
}
memset(MN[i],0, n);
}
return MN;
}
void free_arrays(int **arrays,int m){
if(arrays==NULL){
exit(1);
}
for(int i=0;i< m; i++){
if(arrays[i] ==NULL){
exit(1);
}
free(arrays[i]);
}
free(arrays);
}