Adapted from: “Beej’s Guide to C Programming” by Brian (Beej Jorgensen) Hall: Beej’s Guide to C Programming: 6.6.3 Arrays and Pointers: Changing Arrays in Functions
Program for Demonstrating Changing Arrays in Functions¶
#include <stdio.h>
void double_array(int *a, int len)
{
// Multiply each element by 2
//
// This doubles the values in x in main() since x and a both point
// to the same array in memory!
for (int i = 0; i < len; i++)
a[i] *= 2;
}
int main(void)
{
int x[5] = {1, 2, 3, 4, 5};
double_array(x, 5);
for (int i = 0; i < 5; i++)
printf("%d\n", x[i]); // 2, 4, 6, 8, 10!
}Explanation of the Above Code¶
This C program demonstrates how arrays can be modified within a function by passing them as pointers. Here’s a breakdown of the code:
Code Explanation:¶
1. #include <stdio.h>¶
Includes the standard input/output library to use functions like
printf.
2. Function: double_array¶
void double_array(int *a, int len)
{
for (int i = 0; i < len; i++)
a[i] *= 2;
}Parameters:
int *a: A pointer to the first element of the array.int len: The length of the array.
Purpose:
Iterates through the array using the pointer
a.Multiplies each element of the array by 2 (
a[i] *= 2).
Key Concept:
Since arrays are passed by reference in C, the function modifies the original array in memory.
3. main Function¶
int main(void)
{
int x[5] = {1, 2, 3, 4, 5};
double_array(x, 5);
for (int i = 0; i < 5; i++)
printf("%d\n", x[i]); // 2, 4, 6, 8, 10!
}Steps:
Declares an integer array
xwith 5 elements:{1, 2, 3, 4, 5}.Calls the
double_arrayfunction, passing the arrayxand its length (5).Prints the modified array using a
forloop.
Output:
The array
xis modified in-place bydouble_array, so the output is:2 4 6 8 10
Key Concepts:¶
Passing Arrays to Functions:
In C, when an array is passed to a function, what is actually passed is a pointer to the first element of the array. This allows the function to modify the original array.
Pointer Arithmetic:
The expression
a[i]is equivalent to*(a + i), whereais the pointer to the first element of the array.
In-Place Modification:
Since the function operates on the memory location of the original array, changes made in the function are reflected in the
mainfunction.
This program effectively demonstrates how arrays and pointers work together in C to allow functions to modify array elements directly.### Key Concepts:
Passing Arrays to Functions:
In C, when an array is passed to a function, what is actually passed is a pointer to the first element of the array. This allows the function to modify the original array.
Pointer Arithmetic:
The expression
a[i]is equivalent to*(a + i), whereais the pointer to the first element of the array.
In-Place Modification:
Since the function operates on the memory location of the original array, changes made in the function are reflected in the
mainfunction.
This program effectively demonstrates how arrays and pointers work together in C to allow functions to modify array elements directly.
Similar code found with 2 license types
Compile and Run Code¶
Use Python to Change to Working Directory¶
import os
root_dir = os.getcwd()code_dir = root_dir + "/" + "C_Code"os.chdir(code_dir)build_command = os.system("gcc -o section_6_6_3_changing_arrays_in_functions section_6_6_3_changing_arrays_in_functions.c")exec_status = os.system("./section_6_6_3_changing_arrays_in_functions")2
4
6
8
10