How To Pass An Array By Value In C ?

How To Pass An Array By Value In C ?

In C, when you pass an array as a function argument, you’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.

How to make sure that we have a new copy of the Array when we pass it to function?

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.

Example



// 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 

FAQ- How To Pass An Array By Value In C ?

Q1. How to pass the array variable in C?

Ans. To pass an entire array to a function in C, you only need to provide the array’s name as an argument. For instance, you can use result = calculateSum(num);. However, it’s important to use square brackets [] 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’s structure.

Q2. How to access a value in an array C?

Ans. To access a specific element in an array, use the array name followed by the element’s index enclosed in square brackets. It’s important to note that array indices begin at 0 and go up to size-1. For example, array_name[index] accesses the element at the specified index, with the first element at index 0.

Q3. How do you pass an array value to a method?

Ans. When passing an array as an argument to a method, you should provide only the array name without square brackets. The method’s prototype should be designed to accept an argument of the array type. In your example, the method prototype is as follows: void method_name(int[] array);. This indicates that the method accepts an array of integers as an argument.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment