In C, array name represents address and when we pass an array, we actually pass address and the parameter receiving function always accepts them as pointers (even if we use [], refer this for details).
How to pass array by value, i.e., how to make sure that we have a new copy of array when we pass it to function?
This can be done by wrapping the array in a structure and creating a variable of type of that structure and assigning values to that array. After that, passing the variable to some other function and modifying it as per requirements. Note that array members are copied when passed as parameter, but dynamic arrays are not. So this solution works only for non-dynamic arrays (created without new or malloc).
Let’s see an example to demonstrate the above fact using a C program:
// C program to demonstrate passing an array // by value using structures. #include<stdio.h> #include<stdlib.h> # define SIZE 5 // A wrapper for array to make sure that array // is passed by value. struct ArrayWrapper { int arr[SIZE]; }; // An array is passed by value wrapped in temp void modify(struct ArrayWrapper temp) { int *ptr = temp.arr; int i; // Display array contents printf("In 'modify()', before modification\n"); for (i = 0; i < SIZE; ++i) printf("%d ", ptr[i]); printf("\n"); // Modify the array for (i = 0; i < SIZE; ++i) ptr[i] = 100; // OR *(ptr + i) printf("\nIn 'modify()', after modification\n"); for (i = 0; i < SIZE; ++i) printf("%d ", ptr[i]); // OR *(ptr + i) } // Driver code int main() { int i; struct ArrayWrapper obj; for (i=0; i<SIZE; i++) obj.arr[i] = 10; modify(obj); // Display array contents printf("\n\nIn 'Main', after calling modify() \n"); for (i = 0; i < SIZE; ++i) printf("%d ", obj.arr[i]); // Not changed printf("\n"); return 0; } |
Output:
In 'modify()', before modification 10 10 10 10 10 In 'modify()', after modification 100 100 100 100 100 In 'Main', after calling modify() 10 10 10 10 10
Reference:
http://stackoverflow.com/questions/11158858/c-pass-array-by-value
This article is contributed by MAZHAR IMAM KHAN. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- How to pass a 2D array as a parameter in C?
- When do we pass arguments by reference or pointer?
- Different ways to Initialize all members of an array to the same value in C
- Whatâs difference between âarrayâ and â&arrayâ for âint array[5]â ?
- Result of comma operator as l-value in C and C++
- Increment (Decrement) operators require L-value Expression
- Comparison of a float with a value in C
- gcvt() | Convert float value to string in C
- How to Read and Print an Integer value in C
- Difference between Call by Value and Call by Reference
- Else without IF and L-Value Required Error in C
- Pointer to an Array | Array Pointer
- Difference between pointer to an array and array of pointers
- Jagged Array or Array of Arrays in C with Examples
- Array of Structures vs. Array within a Structure in C/C++
- Why C treats array parameters as pointers?
- Pointer vs Array in C
- Do not use sizeof for array parameters
- What is the difference between single quoted and double quoted declaration of char array?
- Are array members deeply copied?

