0% found this document useful (0 votes)
150 views3 pages

Dynamic Memory Allocation of 2D Array in C

This document discusses four methods for dynamically allocating memory for a 2D array in C: using a single pointer, an array of pointers, a double pointer, and a double pointer with a single malloc call. Each method is demonstrated with sample code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
150 views3 pages

Dynamic Memory Allocation of 2D Array in C

This document discusses four methods for dynamically allocating memory for a 2D array in C: using a single pointer, an array of pointers, a double pointer, and a double pointer with a single malloc call. Each method is demonstrated with sample code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Dynamic memory allocation of 2D array in C

1) Using a single pointer:


A simple way is to allocate memory block of size r*c and access elements using simple pointer
arithmetic.

#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;

for (i = 0; i < r; i++)


for (j = 0; j < c; j++)
printf("%d ", *(arr + i*c + j));

return 0;
}

2) Using an array of pointers


We can create an array of pointers of size r. After creating an array of pointers, we can
dynamically allocate memory for every row.

#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));

// Note that arr[i][j] is same as *(*(arr+i)+j)


count = 0;
for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
arr[i][j] = ++count; // Or *(*(arr+i)+j) = ++count
for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
printf("%d ", arr[i][j]);

return 0;
}

3) Using pointer to a pointer


We can create an array of pointers also dynamically using a double pointer. Once we have an
array pointers allocated dynamically, we can dynamically allocate memory and for every row
like method 2.

#include <stdio.h>
#include <stdlib.h>

int main()
{
int r = 3, c = 4, i, j, count;

int **arr = (int **)malloc(r * sizeof(int *));


for (i=0; i<r; i++)
arr[i] = (int *)malloc(c * sizeof(int));

// Note that arr[i][j] is same as *(*(arr+i)+j)


count = 0;
for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
arr[i][j] = ++count; // OR *(*(arr+i)+j) = ++count

for (i = 0; i < r; i++)


for (j = 0; j < c; j++)
printf("%d ", arr[i][j]);

return 0;
}

4) Using double pointer and one malloc call for all rows

#include<stdio.h>
#include<stdlib.h>

int main()
{
int r=3, c=4;
int **arr;
int count = 0,i,j;
arr = (int **)malloc(sizeof(int *) * r);
arr[0] = (int *)malloc(sizeof(int) * c * r);

for(i = 0; i < c; i++)


arr[i] = (*arr + c * i);

for (i = 0; i < r; i++)


for (j = 0; j < c; j++)
arr[i][j] = ++count; // OR *(*(arr+i)+j) = ++count

for (i = 0; i < r; i++)


for (j = 0; j < c; j++)
printf("%d ", arr[i][j]);

return 0;
}

You might also like