{"id":2627,"date":"2024-05-10T11:05:51","date_gmt":"2024-05-10T11:05:51","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=2627"},"modified":"2024-05-10T11:05:51","modified_gmt":"2024-05-10T11:05:51","slug":"how-to-pass-a-2d-array-as-a-parameter-in-c","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/how-to-pass-a-2d-array-as-a-parameter-in-c\/","title":{"rendered":"How To Pass A 2D Array As A Parameter In C?"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#how-to-pass-a-2-d-array-as-a-parameter-in-c\">How To Pass A 2D Array As A Parameter In C?<\/a><\/li><li ><a href=\"#1-when-both-dimensions-are-available-globally-either-as-a-macro-or-as-a-global-constant\">1) When both dimensions are available globally (either as a macro or as a global constant).\u00a0<\/a><\/li><li ><a href=\"#2-when-only-the-second-dimension-is-available-globally-either-as-a-macro-or-as-a-global-constant\">2) When only the second dimension is available globally (either as a macro or as a global constant)<\/a><\/li><li ><a href=\"#3-if-the-compiler-is-c-99-compatible\">3) If the compiler is C99 compatible\u00a0<\/a><\/li><li ><a href=\"#4-using-a-single-pointer\">4) Using a single pointer\u00a0<\/a><\/li><li ><a href=\"#5-using-the-concept-of-a-pointer-to-an-array\">5) Using the concept of a pointer to an array<\/a><\/li><li ><a href=\"#faq-how-to-pass-a-2-d-array-as-a-parameter-in-c\">FAQ- How To Pass A 2D Array As A Parameter In C?<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-pass-a-2-d-array-as-a-parameter-in-c\">How To Pass A 2D Array As A Parameter In C?<\/h2>\n\n\n\n<p>Passing multidimensional arrays, such as 2D arrays, to functions in C and C++ can indeed be a bit more complex than passing one-dimensional arrays or pointers. The reason for this is that in C and C++, the size of the second (and subsequent) dimensions must be specified, while the size of the first dimension can be left unspecified.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-when-both-dimensions-are-available-globally-either-as-a-macro-or-as-a-global-constant\">1) When both dimensions are available globally (either as a macro or as a global constant).&nbsp;<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\n#include &lt;stdio.h&gt;\nconst int M = 3;\nconst int N = 3;\n \nvoid print(int arr&#91;M]&#91;N])\n{\n    int i, j;\n    for (i = 0; i &lt; M; i++)\n      for (j = 0; j &lt; N; j++)\n        printf(\"%d \", arr&#91;i]&#91;j]);\n}\n \nint main()\n{\n    int arr&#91;]&#91;3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};\n    print(arr);\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1 2 3 4 5 6 7 8 9 \n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2-when-only-the-second-dimension-is-available-globally-either-as-a-macro-or-as-a-global-constant\">2) When only the second dimension is available globally (either as a macro or as a global constant)<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\n#include &lt;stdio.h&gt;\nconst int N = 3;\n \nvoid print(int arr&#91;]&#91;N], int m)\n{\n    int i, j;\n    for (i = 0; i &lt; m; i++)\n      for (j = 0; j &lt; N; j++)\n        printf(\"%d \", arr&#91;i]&#91;j]);\n}\n \nint main()\n{\n    int arr&#91;]&#91;3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};\n    print(arr, 3);\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1 2 3 4 5 6 7 8 9 <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3-if-the-compiler-is-c-99-compatible\">3) If the compiler is C99 compatible&nbsp;<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\n\/\/ The following program works only if your compiler is C99 compatible.\n#include &lt;stdio.h&gt;\n \n\/\/ n must be passed before the 2D array\nvoid print(int m, int n, int arr&#91;]&#91;n])\n{\n    int i, j;\n    for (i = 0; i &lt; m; i++)\n      for (j = 0; j &lt; n; j++)\n        printf(\"%d \", arr&#91;i]&#91;j]);\n}\n \nint main()\n{\n    int arr&#91;]&#91;3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};\n    int m = 3, n = 3;\n    print(m, n, arr);\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1 2 3 4 5 6 7 8 9 \n<\/code><\/pre>\n\n\n\n<p>Note: if the compiler is not C99 compatible, hence, we can use any of the listed methods to pass the variable-sized 2d array.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"4-using-a-single-pointer\">4) Using a single pointer&nbsp;<\/h2>\n\n\n\n<p>We need to typecast the 2D  array while passing to function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\nvoid print(int *arr, int m, int n)\n{\n    int i, j;\n    for (i = 0; i &lt; m; i++)\n      for (j = 0; j &lt; n; j++)\n        printf(\"%d \", *((arr+i*n) + j));\n}\n \nint main()\n{\n    int arr&#91;]&#91;3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};\n    int m = 3, n = 3;\n \n    \/\/ We can also use \"print(&amp;arr&#91;0]&#91;0], m, n);\"\n    print((int *)arr, m, n);\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1 2 3 4 5 6 7 8 9 \n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"5-using-the-concept-of-a-pointer-to-an-array\">5) Using the concept of a pointer to an array<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\n#include &lt;stdio.h&gt;\nconst int M = 3;\n \n \nvoid print(int (*arr)&#91;M])\n{\n    int i, j;\n    for (i = 0; i &lt; M; i++)\n    for (j = 0; j &lt; M; j++)\n        printf(\"%d \", arr&#91;i]&#91;j]);\n}\n \nint main()\n{\n    int arr&#91;]&#91;3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};\n    print(arr);\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1 2 3 4 5 6 7 8 9 \n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-how-to-pass-a-2-d-array-as-a-parameter-in-c\">FAQ- How To Pass A 2D Array As A Parameter In C?<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1697006530919\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. Can you pass a 2D array as a parameter in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. In C, you can pass a 2D array to a function in two ways: by passing the entire array as an argument or by passing the array as a dynamic pointer to the function. The first method involves specifying the size of the second dimension while leaving the first dimension unspecified. The second method uses a dynamic pointer and is suitable for dynamically allocated 2D arrays.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1697006538178\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. How to go through a 2D array in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. To access an element of a two-dimensional array, you need to provide both the row and column indices. In the given statement, it accesses the value of the element located in the first row (index 0) and the third column (index 2) of the &#8220;matrix&#8221; array.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1697006544856\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What is the syntax for 2d array?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans.  The syntax Of 2d Array is <strong>data_type[][] array_name;<\/strong> . In Java, to create a two-dimensional array, you specify the data type of the elements to be stored in the array, followed by two pairs of square brackets, and then provide the name of the array.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>How To Pass A 2D Array As A Parameter In C? Passing multidimensional arrays, such as 2D arrays, to functions in C and C++ can indeed be a bit more complex than passing one-dimensional arrays or pointers. The reason for this is that in C and C++, the size of the second (and subsequent) dimensions &#8230; <a title=\"How To Pass A 2D Array As A Parameter In C?\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/how-to-pass-a-2d-array-as-a-parameter-in-c\/\" aria-label=\"More on How To Pass A 2D Array As A Parameter In C?\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":2629,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[444],"class_list":["post-2627","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-how-to-pass-a-2d-array-as-a-parameter-in-chow-to-pass-a-2d-array-as-a-parameter-in-c","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2627","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=2627"}],"version-history":[{"count":9,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2627\/revisions"}],"predecessor-version":[{"id":10715,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2627\/revisions\/10715"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/2629"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=2627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=2627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=2627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}