Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Section 6.6.3 Arrays and Pointers: Changing Arrays in Functions

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

Brian (Beej Jorgensen) Hall Website

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:

    1. Declares an integer array x with 5 elements: {1, 2, 3, 4, 5}.

    2. Calls the double_array function, passing the array x and its length (5).

    3. Prints the modified array using a for loop.

  • Output:

    • The array x is modified in-place by double_array, so the output is:

      2
      4
      6
      8
      10

Key Concepts:

  1. 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.

  2. Pointer Arithmetic:

    • The expression a[i] is equivalent to *(a + i), where a is the pointer to the first element of the array.

  3. In-Place Modification:

    • Since the function operates on the memory location of the original array, changes made in the function are reflected in the main function.

This program effectively demonstrates how arrays and pointers work together in C to allow functions to modify array elements directly.### Key Concepts:

  1. 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.

  2. Pointer Arithmetic:

    • The expression a[i] is equivalent to *(a + i), where a is the pointer to the first element of the array.

  3. In-Place Modification:

    • Since the function operates on the memory location of the original array, changes made in the function are reflected in the main function.

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