oke I was coding some stuff involving 2d array allocating i normally know how to do it in main coz you don't need to pass adr main ptr that get's allocated but when i made it i got somewhat confused I don't know why it went wrong
#include <stdio.h>
#include <windows.h>
BOOL AllocateMemory(int ***iArray,int rows,int cols) {
*iArray = (int**)calloc(rows,sizeof(int));
if(!*iArray)
return false;
for(int i =0;i < cols;i++) {
**iArray = (int*)calloc(cols,sizeof(int));
if(!**iArray)
return false;
}
return true;
}
void FreeAllocated(int **iArray,int rows,int cols) {
for(int i = 0; i < rows;i++) {
for(int x = 0;x < cols;x++)
free(*iArray);
free(iArray);
}
return;
}
void ShowArr(int **iArray,int rows,int cols) {
for(int i = 0; i < rows;i++)
for(int x = 0 ; x < cols ;x++)
printf("%d\n",iArray[i][x]);
}
int main(void)
{
int **iArray;
if(AllocateMemory(&iArray,3,3)) {
ShowArr(iArray,3,3);
FreeAllocated(iArray,3,3);
}
getchar();
return 0;
}
ShowArr should show all 0 but it shows first i made allocating ptr to ptr then after that in loop i made allocate ptr to int only which should be right ..