I know its very naive question, but i am not able to understand what the following code does.
#include <malloc.h>
#define MAXROW 3
#define MAXCOL 4
int main(){
int (*p)[MAXCOL];
p = (int (*)[MAXCOL])malloc(MAXROW*sizeof(*p));
}
Please provide a complete explanation including the type and size of p.
It is just for learning purpose. I am not using this code in any real application.
As far as I can tell, it’s gibberish. You probably meant
(int(*)[MAXCOL]).In C it means that the programmer who wrote it doesn’t know how void pointer typecasts work.
In C++ it means that you are allocating an array of arrays. p is an array pointer, so *p is an array of size MAXCOL, and you allocate MAXROW such arrays. The result is a “mangled” 2D array. The avantage of using this rather obscure syntax is that you get a 2D array which has every cell in adjacent memory, something you wouldn’t achieve with the more commonly seen pointer-to-pointer dynamic 2D array.