GeeksforGeeks » Interview Questions
Microsoft Interview Question for Software Engineer/Developer about CPuzzles
(9 posts)-
allocate 2D array dynamically with min no. of malloc calls.
-
now answer to this question depends whether u want to excess array using [i][j] notation or not
-
This can be done using consecutive memory allocation and then playing with pinter arithmetic.
Problem with this approach is that it is not possible to realloc it row/column wise going ahead. -
int a[10][20];
a=(char **)malloc(sizeof(char[10][20]; -
-
int (*p)[MAXCOL];
p=(int (*)[MAXCOL]) malloc (MAXROW * sizeof(*p)); -
int *ptr;
ptr=(int*)malloc(rows*clumns*sizeof(int)); -
first allocate memory for **p, and then for each *p assign memory in a loop.
-
@sahil316:
when we have written int a[10][20] memory is already allocated to array a, why do we need malloc, infact i dont think it is dynamic memory allocation at all.
Reply
You must log in to post.