{"id":2638,"date":"2024-05-10T11:06:24","date_gmt":"2024-05-10T11:06:24","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=2638"},"modified":"2024-05-10T11:06:24","modified_gmt":"2024-05-10T11:06:24","slug":"how-to-pass-an-array-by-value-in-c","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/how-to-pass-an-array-by-value-in-c\/","title":{"rendered":"How To Pass An Array By Value 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-an-array-by-value-in-c\">How To Pass An Array By Value In C ?<\/a><\/li><li ><a href=\"#how-to-make-sure-that-we-have-a-new-copy-of-the-array-when-we-pass-it-to-function\">How to make sure that we have a new copy of the Array when we pass it to function?<\/a><\/li><li ><a href=\"#example\">Example<\/a><\/li><li ><a href=\"#faq-how-to-pass-an-array-by-value-in-c\">FAQ- How To Pass An Array By Value In C ?<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-pass-an-array-by-value-in-c\">How To Pass An Array By Value In C ?<\/h2>\n\n\n\n<p>In C, when you pass an array as a function argument, you&#8217;re actually passing a pointer to the first element of the array. The array name itself represents the address of the first element. The receiving function accepts this argument as a pointer, not as a whole array. This is because arrays decay into pointers in most contexts when passed as arguments to functions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-make-sure-that-we-have-a-new-copy-of-the-array-when-we-pass-it-to-function\">How to make sure that we have a new copy of the Array when we pass it to function?<\/h2>\n\n\n\n<p>You can put an array inside a structure, make a variable of that structure type, and then pass it to functions. This way, you can control and manage the array within a structured context, making it easier to modify the array as required while keeping related information together.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example\">Example<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\n\/\/ C program to demonstrate passing an array \n\/\/ by value using structures. \n#include&lt;stdio.h&gt; \n#include&lt;stdlib.h&gt; \n  \n# define SIZE 5 \n  \n\/\/ A wrapper for array to make sure that array \n\/\/ is passed by value. \nstruct ArrayWrapper \n{ \n    int arr&#91;SIZE]; \n}; \n  \n\/\/ An array is passed by value wrapped in temp \nvoid modify(struct ArrayWrapper temp) \n{ \n    int *ptr = temp.arr; \n    int i; \n  \n    \/\/ Display array contents \n    printf(\"In 'modify()', before modification\\n\"); \n    for (i = 0; i &lt; SIZE; ++i) \n        printf(\"%d \", ptr&#91;i])\n printf(\"\\n\"); \n  \n    \/\/ Modify the array \n    for (i = 0; i &lt; SIZE; ++i) \n        ptr&#91;i] = 100; \/\/ OR *(ptr + i) \n  \n    printf(\"\\nIn 'modify()', after modification\\n\"); \n    for (i = 0; i &lt; SIZE; ++i) \n        printf(\"%d \", ptr&#91;i]); \/\/ OR *(ptr + i) \n} \n  \n\/\/ Driver code \nint main() \n{ \n    int i; \n    struct ArrayWrapper obj; \n    for (i=0; i&lt;SIZE; i++) \n        obj.arr&#91;i] = 10; \n  \n    modify(obj); \n  \n    \/\/ Display array contents \n    printf(\"\\n\\nIn 'Main', after calling modify() \\n\"); \n    for (i = 0; i &lt; SIZE; ++i) \n        printf(\"%d \", obj.arr&#91;i]); \/\/ Not changed \n  \n    printf(\"\\n\"); \n  \n    return 0; \n} <\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>In 'modify()', before modification\n10 10 10 10 10 \n\nIn 'modify()', after modification\n100 100 100 100 100 \n\nIn 'Main', after calling modify() \n10 10 10 10 10 <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-how-to-pass-an-array-by-value-in-c\">FAQ- How To Pass An Array By Value 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-1697019695257\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. How to pass the array variable in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. To pass an entire array to a function in C, you only need to provide the array&#8217;s name as an argument. For instance, you can use <code>result = calculateSum(num);<\/code>. However, it&#8217;s important to use square brackets <code>[]<\/code> in the function definition to indicate to the compiler that you are passing a one-dimensional array to the function. This informs the compiler about the array&#8217;s structure.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1697019707766\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. How to access a value in an array C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. To access a specific element in an array, use the array name followed by the element&#8217;s index enclosed in square brackets. It&#8217;s important to note that array indices begin at 0 and go up to size-1. For example, <code>array_name[index]<\/code> accesses the element at the specified <code>index<\/code>, with the first element at index 0.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1697019715813\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. How do you pass an array value to a method?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. When passing an array as an argument to a method, you should provide only the array name without square brackets. The method&#8217;s prototype should be designed to accept an argument of the array type. In your example, the method prototype is as follows: <code>void method_name(int[] array);<\/code>. This indicates that the method accepts an array of integers as an argument.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>How To Pass An Array By Value In C ? In C, when you pass an array as a function argument, you&#8217;re actually passing a pointer to the first element of the array. The array name itself represents the address of the first element. The receiving function accepts this argument as a pointer, not as &#8230; <a title=\"How To Pass An Array By Value In C ?\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/how-to-pass-an-array-by-value-in-c\/\" aria-label=\"More on How To Pass An Array By Value In C ?\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":2639,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[446],"class_list":["post-2638","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-how-to-pass-an-array-by-value-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\/2638","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=2638"}],"version-history":[{"count":7,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2638\/revisions"}],"predecessor-version":[{"id":10717,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2638\/revisions\/10717"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/2639"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=2638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=2638"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=2638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}