How to pass an array by value in C ?
Last Updated :
21 Dec, 2018
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:
#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;
printf ( "\nIn 'modify()', after modification\n" );
for (i = 0; i < SIZE; ++i)
printf ( "%d " , ptr[i]);
}
int main()
{
int i;
struct ArrayWrapper obj;
for (i=0; i<SIZE; i++)
obj.arr[i] = 10;
modify(obj);
printf ( "\n\nIn 'Main', after calling modify() \n" );
for (i = 0; i < SIZE; ++i)
printf ( "%d " , obj.arr[i]);
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
Share your thoughts in the comments
Please Login to comment...