How to pass an array by value?
This can be accomplished by encasing the array in a structure, creating a variable of the structure's type, and assigning values to the array. The variable is then passed to another function, which modifies it according to the requirements.
Let's look at an example using a C Program code to demonstrate the above fact:
#include<stdio.h>
#include<stdlib.h>
# define SIZE 5
struct ArrayWrapper
{
int arr[SIZE];
};
void modify(struct ArrayWrapper temp)
{
int *ptr = temp.arr;
int i;
printf("In 'modify()', before modification\n");
for (i = 0; i < SIZE; ++i)
printf("%d ", ptr[i]);
printf("\n");
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)
}
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:

A wrapper (ArrayWrapper) if used for array to make sure that array is passed by value. The array is passed by value wrapped in temp. We can see that eventhough we modify the array elements in modify() method, it is not reflected in main().
Check out this problem - Search In Rotated Sorted Array
You can also read about dynamic array in c.
FAQs
What is call by value?
When we pass variable values to a function when we call it the "Call By Values" function. The value of each variable in the calling function is copied into the called function's corresponding dummy variables in this method. Changes to the dummy variables in the called function have no effect on the values of actual variables in the calling function when using this method.
What is call by reference?
Instead of passing the values of variables to a function, we pass the address of variables (location of variables) to the "Call By References" function. The addresses of actual variables in the calling function are copied into dummy variables in the called function using this method. We would have access to the actual variables and thus be able to manipulate them using this method, which uses addresses.
How do you access the elements of an array?
You can access elements of an array by indices.
Suppose you declared an array arr as above. The first element is arr[0], the second element is arr[1], and so on.
Suppose arr is an array of type float, and the starting address of arr[0] is 2120d. Then, what is the next address?
Suppose the starting address of arr[0] is 2120d. Then, the next address, arr[1], will be 2124d, the address of arr[2] will be 2128d, and so on. It's because the size of a float is 4 bytes.
How do arrays store data?
Data stored in arrays is traditionally kept in the heap of computer memory, just like strings. When you use a statement like 'int x = 1;' to store a basic integer in a variable, the value is stored on the stack.
Conclusion
In this article, we learned how to pass an array by value in C and saw its practical demonstration.
We hope that this blog has helped you enhance your knowledge regarding passing an array by value and if you would like to learn more about arrays, and their implementation and go deeper into 2D arrays, check out our articles on Coding Ninjas Studio.
Recommended problems -
Do upvote our blog to help other ninjas grow. Happy Coding!