Section 6.6.2 Arrays and Pointers: Passing Single-Dimensional Arrays to Functions#
Adapted from: “Beej’s Guide to C Programming” by Brian (Beej Jorgensen) Hall: Beej’s Guide to C Programming: 6.6.2 Arrays and Pointers: Passing Single Dimensional Arrays to Functions
Brian (Beej Jorgensen) Hall Website
Program for Demonstrating Passing Single-Dimensional Arrays to Functions#
#include <stdio.h>
// Passing as a pointer to the first element
void times2(int *a, int len)
{
for (int i = 0; i < len; i++)
printf("%d\n", a[i] * 2);
}
// Same thing, but using array notation
void times3(int a[], int len)
{
for (int i = 0; i < len; i++)
printf("%d\n", a[i] * 3);
}
// Same thing, but using array notation with size
void times4(int a[5], int len)
{
for (int i = 0; i < len; i++)
printf("%d\n", a[i] * 4);
}
int main(void)
{
int x[5] = {11, 22, 33, 44, 55};
times2(x, 5);
times3(x, 5);
times4(x, 5);
}
Explanation of the Above Code#
This C program demonstrates how to pass single-dimensional arrays to functions in different ways and perform operations on their elements. 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: times2
#
void times2(int *a, int len)
{
for (int i = 0; i < len; i++)
printf("%d\n", a[i] * 2);
}
This function takes a pointer to the first element of an array (
int *a
) and its length (int len
).It iterates through the array using the pointer and multiplies each element by 2, printing the result.
3. Function: times3
#
void times3(int a[], int len)
{
for (int i = 0; i < len; i++)
printf("%d\n", a[i] * 3);
}
This function uses array notation (
int a[]
) to accept the array and its length.It performs the same operation as
times2
, but multiplies each element by 3.
4. Function: times4
#
void times4(int a[5], int len)
{
for (int i = 0; i < len; i++)
printf("%d\n", a[i] * 4);
}
This function specifies the array size in its parameter (
int a[5]
), but this size is not enforced by the compiler.It multiplies each element by 4 and prints the result.
5. main
Function#
int main(void)
{
int x[5] = {11, 22, 33, 44, 55};
times2(x, 5);
times3(x, 5);
times4(x, 5);
}
Declares an integer array
x
with 5 elements:{11, 22, 33, 44, 55}
.Calls the three functions (
times2
,times3
,times4
) with the arrayx
and its length (5
).
Key Concepts:#
Passing Arrays to Functions:
Arrays are passed to functions as pointers to their first element. For example,
x
intimes2(x, 5)
is equivalent to&x[0]
.
Array Notation vs Pointer Notation:
int *a
andint a[]
are functionally equivalent when used as parameters. Both represent a pointer to the first element of the array.
Output:
The program prints the elements of the array multiplied by 2, 3, and 4, respectively:
22 44 66 88 110 33 66 99 132 165 44 88 132 176 220
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_2_passing_single_dimensional_arrays_to_functions section_6_6_2_passing_single_dimensional_arrays_to_functions.c")
exec_status = os.system("./section_6_6_2_passing_single_dimensional_arrays_to_functions")
22
44
66
88
110
33
66
99
132
165
44
88
132
176
220