Acces 2D Array
Acces 2D Array
BY AMLENDRAON
https://aticleworld.com/access-two-dimensional-array-using-pointers-in-c/
I have written a lot of articles on array and pointer if you want you can see this link, C Tutorial.
Nowadays many students ask me a question that how to access a multidimensional array with a pointer.
I have replied many students but every week I found this question in my Inbox.
So I have decided to write an article on how to access a multidimensional array with a pointer. I am
assuming you are already familiar with a multidimensional array, if you don’t have the knowledge of
array, then you should check this article, brief introduction of an array.
acData [i][j] = *(acData [i]+j); ————————–>2D array in form of 1D array and pointer.
Note: An array elements stored in a consecutive memory block, so we can access the elements of the
array using the pointer.
Suppose int aiData[3][3] is a 2D array that has 3 rows and 3 columns. If you need to access the 2nd
element of 1 row in aiData, then calculates its offset that will be (1 * coloumb_number) + 2 ). Now to
access the element just add the offset in array base address and dereference it.
Note: Array index always start with 0, so 2nd means third element.
Add offset in array base address => (int *)aiData + offset; //here
Note: General expression to calculates offset for 2D array is that, (ithRow * Total_number_Coloumb)+
jthColoumb).
#include <stdio.h>
#define R#include <stdio.h>
#define ROW 3
#define COL 3
int main(void) {
// 2d array
return 0;
We know that array element is stored in the contiguous form so we can also access the elements of the
two-dimensional array to the calculate the total number of cells.
#include <stdio.h>
#define ROW 3 // number of rows in array
int main(void)
// 2d array
}
return 0;
If you want to learn more about the c language, here 10 Free days (up to 200 minutes) C video course for you.
#include <stdio.h>
#define ROW 3
#define COL 3
int main(void)
// 2d array
return 0;
Similar to the two-dimensional array we can access three, fourth, … etc dimensional array using the
pointers.
How to dynamically allocate a 2D
array in C?
Following are different ways to create a 2D array on heap (or dynamically allocate a 2D array).
In the following examples, we have considered ‘r‘ as number of rows, ‘c‘ as number of columns and we
created a 2D array with r = 3, c = 4 and following values
1 2 3 4
5 6 7 8
9 10 11 12
#include <stdio.h>
#include <stdlib.h>
int main()
{
int r = 3, c = 4;
int *arr = (int *)malloc(r * c * sizeof(int));
int i, j, count = 0;
for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
*(arr + i*c + j) = ++count;
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10 11 12
#include <stdio.h>
#include <stdlib.h>
int main()
{
int r = 3, c = 4, i, j, count;
int *arr[r];
for (i=0; i<r; i++)
arr[i] = (int *)malloc(c * sizeof(int));
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10 11 12
#include <stdio.h>
#include <stdlib.h>
int main()
{
int r = 3, c = 4, i, j, count;
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10 11 12
#include<stdio.h>
#include<stdlib.h>
int main()
{
int r=3, c=4, len=0;
int *ptr, **arr;
int count = 0,i,j;
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10 11 12
1) When both dimensions are available globally (either as a macro or as a global constant).
#include <stdio.h>
const int M = 3;
const int N = 3;
int main()
{
int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
print(arr);
return 0;
}
Output:
1 2 3 4 5 6 7 8 9
#include <stdio.h>
const int N = 3;
int main()
{
int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
print(arr, 3);
return 0;
}
Output:
1 2 3 4 5 6 7 8 9
The above method is fine if second dimension is fixed and is not user specified. The following methods
handle cases when second dimension can also change.
int main()
{
int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int m = 3, n = 3;
print(m, n, arr);
return 0;
}
Output on a C99 compatible compiler:
1 2 3 4 5 6 7 8 9
If compiler is not C99 compatible, then we can use one of the following methods to pass a variable
sized 2D array.
#include <stdio.h>
void print(int *arr, int m, int n)
{
int i, j;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
printf("%d ", *((arr+i*n) + j));
}
int main()
{
int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int m = 3, n = 3;