0% found this document useful (0 votes)
10 views11 pages

Passing Arrays to Functions in C

This document explains how to pass arrays to functions in C, highlighting the differences between passing by value and by reference. It includes examples demonstrating how changes to array elements can affect the original array based on the passing method used. The document also covers passing entire arrays using pointer notation and the implications of these methods on program behavior.

Uploaded by

rhulsung
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views11 pages

Passing Arrays to Functions in C

This document explains how to pass arrays to functions in C, highlighting the differences between passing by value and by reference. It includes examples demonstrating how changes to array elements can affect the original array based on the passing method used. The document also covers passing entire arrays using pointer notation and the implications of these methods on program behavior.

Uploaded by

rhulsung
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

The purpose of this document is to demonstrate

various ways to pass arrays to functions in the


C programming language. Understanding how
arrays are passed to functions is essential in C, as
it directly impacts whether changes made within a
function affect the original array in the calling
function (main()).
Through a set of simple and clear C programs, this
document explains the difference between:
 Passing array elements by value vs. by
reference,
 Passing the entire array using pointer or array
notation,
 And how these methods impact the behavior
of the program.

Program-1:

#include <stdio.h>
// Function to modify a single element (passed by
value)
void modifyElement(int element) {
element += 10; // Modify the copy
}

int main() {
int a[] = {1, 2, 3, 4, 5};
int n = sizeof(a)/sizeof(a[0]);

printf("Before function call:\n");


for(int i = 0; i < n; i++) {
printf("%d ", a[i]);
}

// Call function for each element (by value)


modifyElement(a[0]);
modifyElement(a[1]);
modifyElement(a[2]);
modifyElement(a[3]);
modifyElement(a[4]);
}

printf("\nAfter function call:\n");


for(int i = 0; i < n; i++) {
printf("%d ", a[i]); // Array remains unchanged
}

return 0;
}
Summary: In this program, the function is
called separately for each array element. If
the array contains 5 elements, the function will
be called 5 times. Each call sends one value
from the array. Since the values are passed
by value, any changes made inside the
function do not affect the original array.
Program -2:
#include <stdio.h>

// Function to modify a single element (passed by


value)
void modifyElement(int element) {
element += 10; // Modify the copy
}

int main() {
int a[] = {1, 2, 3, 4, 5};
int n = sizeof(a)/sizeof(a[0]);

printf("Before function call:\n");


for(int i = 0; i < n; i++) {
printf("%d ", a[i]);
}

// Call function for each element (by value)


for(int i = 0; i < n; i++) {
modifyElement(a[i]); // Pass only value
(copy)
}

printf("\nAfter function call:\n");


for(int i = 0; i < n; i++) {
printf("%d ", a[i]);
}

return 0;
}

Summary: This program is similar to the first one,


but the function calls are made inside a loop to
reduce repetition. Each array element is still
passed by value, and therefore, the original array
remains unchanged after the function calls.

Program -3

#include <stdio.h>
// Function to modify a single array element
void modifyElement(int *element) {
*element += 10; // Add 10 to the value
}

int main() {
int a[] = {1, 2, 3, 4, 5};
int n = sizeof(a)/sizeof(a[0]);

printf("Before function call:\n");


for(int i = 0; i < n; i++) {
printf("%d ", a[i]);
}

// Call function for each element


for(int i = 0; i < n; i++) {
modifyElement(&a[i]); //
Pass address of each element
}
printf("\nAfter function call:\n");
for(int i = 0; i < n; i++) {
printf("%d ", a[i]);
}

return 0;
}

Summary: Instead of passing values, this program


passes the address of each array element using
the & operator. The function receives a pointer to
each element. Since elements are passed by
reference, modifications inside the function
affect the original array.

Program-4:

#include <stdio.h>
void modifyArray1(int arr[], int size) {
for(int i = 0; i < size; i++) {
arr[i] += 10; // Add 10 to each element
}
}

int main() {
int a[] = {1, 2, 3, 4, 5};
int n = sizeof(a)/sizeof(a[0]);

printf("Before function call:\n");


for(int i = 0; i < n; i++) {
printf("%d ", a[i]);
}

modifyArray1(a, n);

printf("\nAfter function call:\n");


for(int i = 0; i < n; i++) {
printf("%d ", a[i]);
}

return 0;
}
Summary: In this program, the entire array is
passed to the function by sending its base
address. The function receives a pointer to the first
element. Inside the function, a loop is used to
access and modify all elements of the array. Any
changes done inside the function are reflected in
the original array.

Program-5:

#include <stdio.h>

void modifyArray2(int *arr, int size) {


for(int i = 0; i < size; i++) {
arr[i] *= 2; // Multiply each element by 2
}
}

int main() {
int a[] = {6, 7, 8, 9, 10};
int n = sizeof(a)/sizeof(a[0]);

printf("Before function call:\n");


for(int i = 0; i < n; i++) {
printf("%d ", a[i]);
}

modifyArray2(a, n);

printf("\nAfter function call:\n");


for(int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
return 0;
}

Summary: In this program, the function parameter


is defined using pointer notation (int *arr). The
array is passed as a single unit, and the pointer
allows the function to access and modify all
elements. Since arrays are naturally passed by
reference in C, modifications inside the function
affect the array in main().

You might also like