Many times you need to allocate dynamic memory for a multidimensional array in C language. This article deals with using pointers for dynamic memory allocation for two dimensional array , which can further be extended for higher dimensions.
Example of situation when u require 2-D dynamic allocation :
1. Take the input dimension of an matrix form the user and then get input values and print them.
2. Print a magic square after taking dimensions form user.
3. Implementing Hash Table after taking hash value form user.
I will try to explain the concept by solving the first problem (simply coz its the easy to solve :) and also for others to understand )
Problem Statement : Take the input dimension of an matrix form the user and then get input values and print them.
first i will paste a working solution and the explain it :
i tried to copy paste the code but it gave some html error ... so im pasting the image ..... will soon find some way to post the code :)
please right click and open the image in a tab to see the code
Problem is we need to take input from the user at run time , so we cannot declare a 2-D array of fixed dimensions before hand , we need to do it at runtime , using dynamic memory allocation.
For this we use the concept of pointer to a pointer or Double pointer.
Now to revise the basics we can use a normal pointer to allocate memory for an array , i.e. a single dimension array. It can be done in following manner.
printf("enter the size of array");
what happened above is when i said int *array a pointer named array was assigned to point to a location where some int is supposed to reside. But by writing
array=(int *)(malloc(sizeof(int)*arraysize)) i have allocated memory dynamically equal to the arraysize given by the user, where each element is integer type.
if we analyze the right hand side of the statement:
(int*) typecast the memory allocated to be a pointer to a integer, since array is of that type .
malloc(sizeof(int)*arraysize) dynamically allocates a memory of elements equal to arraysize and each element of size int , since sizeof(int) is used.
so in the end what we have is a single dimension array, whose base address is pointed by pointer array.
Now coming to the code that i have posted above for 2-D array :
the part of the code that deals with the 2-D dynamic allocation is :
these three lines are the only lines which u need to understand to get the concept :
Line 1: matrix=(int**)(malloc(sizeof(int*)*no_of_rows));
The variable matrix which is a integer pointer to a pointer is allocated memory allocated equal to no_of_rows and each of type pointer to an integer. So what we have now is an Array of integer Pointers of size equal to no_of_rows .
since matrix is a double pointer so we need to typecast the memory allocated to its type by (int**) and since we are allocating memory for array of pointers so we use sizeof(int*) .
Line 2 :
this is a simple for loop which is responsible to run the line below it no_of_rows time, so that each integer pointer from Array of Pointer can point to integer array itself .
Line 3 : *(matrix+i)=(int*)(malloc(sizeof(int)*no_of_column));
By *(matrix+i) we go to each integer pointer in the array of pointers for which we allocated memory in the Line: 1.
By (int*)(malloc(sizeof(int)*no_of_column)) we first typecast the allocated memory since *(matrix+i) is of type integer pointer we use (int*) and now we want to store integers in the allocated space to we use sizeof(int) .
That all the memory is allocated, rest of program is simple input and display.
For any more clarification feel free to comment.