Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Array Parameters as Pointers
3.
How to pass an array by value?
4.
FAQs
4.1.
What is call by value?
4.2.
What is call by reference?
4.3.
How do you access the elements of an array?
4.4.
Suppose arr is an array of type float, and the starting address of arr[0] is 2120d. Then, what is the next address? 
4.5.
How do arrays store data?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Pass an Array by Value

Author Toohina Barua
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

An array is a collection of similar data items stored at contiguous memory locations that can be accessed randomly using array indices in C or any programming language. They can be used to store a collection of primitive data types of any type, such as int, float, double, char, and so on.
When we pass an array in C, we are actually passing an address, and the parameter receiving function always accepts them as pointers, even if we use []. But, how should we pass an array by value? This article will show just that. So let’s dive in!

Also Read About, Sum of Digits in C, C Static Function

Array Parameters as Pointers

Array parameters are primarily treated as pointers to in C. This is done:

  • To improve the code's efficiency
  • in order to save time


In terms of both memory and time, copying the array data is inefficient; and most of the time, when we pass an array, our intention is to simply refer to the array we're interested in, not to make a copy of it.

Let’s understand this using a coding example and practice it on online C compiler.

#include <stdio.h>


void findSum1(int arr[])
{
int sum = 0;
int i;
for (i = 0; i < 5; i++)
sum = sum + arr[i];
printf("The sum of the array is: %d\n", sum);
}


void findSum2(int* arr)
{
int sum = 0;
int i;
for (i = 0; i < 5; i++)
sum = sum + arr[i];
printf("\nThe sum of the array is: %d \n", sum);
}


int main()
{
int arr[5] = { 1, 2, 3, 4, 5 };


findSum1(arr);
findSum2(arr);
return 0;
}


Output:


In the code above, we can clearly see that the array parameters are being used as pointers as the output of both the functions, findSum1() and findSum2() are the same.

You can also read about the dynamic arrays in c, And Tribonacci Series

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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!

Previous article
Accessing an array out of bounds in C/C++
Next article
Pointers in C
Live masterclass