how to pass a dynamic 2d array to a function c

Solutions on MaxInterview for how to pass a dynamic 2d array to a function c by the best coders in the world

showing results for - "how to pass a dynamic 2d array to a function c"
Montserrat
29 May 2017
1#include <stdio.h>
2#include <stdlib.h>
3
4// Here the parameter is an array of pointers
5void assign(int** arr, int m, int n)
6{
7	for (int i = 0; i < m; i++) {
8		for (int j = 0; j < n; j++) {
9			arr[i][j] = i + j;
10		}
11	}
12}
13
14// Program to pass 2D array to a function in C
15int main(void)
16{
17	int m = 5;
18	int n = 5;
19
20	// dynamically create array of pointers of size m
21	int **arr = (int **)malloc(m * sizeof(int *));
22
23	// dynamically allocate memory of size n for each row
24	for (int r = 0; r < m; r++)
25		arr[r] = (int *)malloc(n * sizeof(int));
26
27	assign(arr, m, n);
28
29	// print 2D array
30	for (int i = 0; i < m; i++) {
31		for (int j = 0; j < n; j++) {
32			printf("%3d", arr[i][j]);
33		}
34		printf("\n");
35	}
36
37	// deallocate memory
38	for (int i = 0; i < m; i++)
39		free(arr[i]);
40	free(arr);
41
42	return 0;
43}
queries leading to this page
dynamic 2d array in cpass 2d array in cpassing 2d array by reference in cpassing 2d array to function in cdynamic 2d array c using 22calloc 22how to pass a 2d array using pointer in chow to create 2d array in c dynamicallyhow to pass a dynamic 2d array to a function in c 2b 2bhow to pass 2d array to function in c 2b 2bhow to pass a delcared 2d array into a function ctpassing wo dimensional array as pointer cc how to pass 2d array to functionpass 2 dimensional array to function c 2b 2bpassing 2d char array to function in c using pointers2d array pass by reference chow to passs through a 2d array in c through a function dynamically2d dynamic array cpassing a 2d array chow to pass 2d dynamic array to function c 2b 2bpassing 2d array to function cdynamic 2d array chow to pass an 2d array as parameter in chow to pass a 2d array in a function cc pass a 2d array to functionhow to pass a 2d array to a function in cdynamic alocate 2d array in cpass a 2d array to a function cc 2d array dynamiccreating a 2d array dynamically in chow to give value to 2d array in c2d array using void in cpassing 2d dynamic array to function c 2b 2bhow to pass an 2d array to a function in cc function with 2d arraypass by reference 2d array cpass 2d array into function cc create 2d array dynamicallypassing 2d array as a parameter to a function in cpass 2d array in fuction in cpass 2d array to function cpassing 2d arry in function using pointersc two dimensional array function pointerpassing 2d arrays to functions in cpass array 2d function cpass 2 d string array to function cpass 2d dynamic array to function c 2b 2bpassing an 2d array to a function in cc pass 2d array to functionpassing a 2d array in cc pass 2d array to function as pointerhow to create a 2d array dynamically in cc program to create 2d array dynamicallysending a 2d array to a function in cpassing 2d array to a function in c 7chow to pass a dynamic 2d array choww to pass a 2d array as argument in chow to pass 2d arrays in functions in chow to pass a 2d array to a function chow to pass 2d array to function in cpass 2d array to functions in chow to pass 2d array in c functionhow to pass 2d array as argument in cpassing 2d array in c as pointer1d dynamic array as 2d collumns cpass 2d array as parameter cpassing matrix as a pointer chow to pass 2d array in function in c2d array in c dynamic how to pass in a 2d array in cpassing 2d array to function in c using pointerscreating a 2d array in c dynamicallyhow to make dynamic 2d array of dynamic arrays in chow to pass a 2d array through a function in chow to easily send 2d array in function chow to pass 2d matrix in function in c 3fhow to make 2d dynamic array in chow to pass a 2d array into a function in chow to send a 2d array to a function in cone dimensional dynamic array c2d array c dynamicc pass a 2d array to a functionhow to pass a dynamic 2d array to a function cpass 2d array to function c pointerhow to pass 2d array in a function in cpass 2d array to function in chow to pass 2d array by reference in cpass 2d array in function cc 2d dynamic arraypassing a function to a 2d array using pointerspass a 2d array as a parameter in cc passing 2d array to functionpass dynamic 2d array to function c 2b 2bdynamic 2d array to functionc dynamic 2d arraypass dynamincally allocated 2d array to function2d dynamic array in cdynamic 2d array c using callocpass a 2d array to c funchow to pass 2d array in cc pass pointer of 2d array to functionhow to pass 2d array as parameter in c3 dimensional dynamic array in cpassing an empty 2d array to a function cc programming pass a 2d array to functionhow to create a 2d array in c dynamicallypass a 2d array to a function in chow to pass a dynamic 2d array to a function c