Section 6.6.4 Arrays and Pointers: Passing Multidimensional Arrays to Functions#
Adapted from: “Beej’s Guide to C Programming” by Brian (Beej Jorgensen) Hall: Beej’s Guide to C Programming: 6.6.4 Arrays and Pointers: Passing Multidimensional Arrays to Functions
Brian (Beej Jorgensen) Hall Website
Program for Demonstrating Passing Multidimensional Arrays to Functions#
#include <stdio.h>
void print_2D_array(int a[2][3])
{
for (int row = 0; row < 2; row++) {
for (int col = 0; col < 3; col++)
printf("%d ", a[row][col]);
printf("\n");
}
}
int main(void)
{
int x[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
print_2D_array(x);
}
Explanation of the Above Code#
This C program demonstrates how to pass a 2D array to a function and print its elements. Below is a detailed explanation of each part of the code:
1. #include <stdio.h>
#
This line includes the Standard Input/Output Library in the program.
It allows the use of functions like
printf
for printing output to the console.
2. Function Definition: print_2D_array
#
void print_2D_array(int a[2][3])
{
for (int row = 0; row < 2; row++) {
for (int col = 0; col < 3; col++)
printf("%d ", a[row][col]);
printf("\n");
}
}
Purpose: This function takes a 2D array as input and prints its elements row by row.
Key Points:#
Parameter:
int a[2][3]
The function accepts a 2D array with 2 rows and 3 columns.
The dimensions
[2][3]
must be explicitly specified in the function signature to correctly access the array elements.
Logic:
The function uses nested
for
loops to iterate through the 2D array:The outer loop iterates over the rows (
row
index).The inner loop iterates over the columns (
col
index).
Each element of the array is accessed using
a[row][col]
and printed usingprintf("%d ", a[row][col]);
.After printing all elements in a row, a newline (
\n
) is printed to move to the next row.
Example Execution:#
For the array:
1 2 3 4 5 6
The outer loop runs twice (for 2 rows).
The inner loop runs three times for each row (for 3 columns).
The elements are printed in the following order:
1 2 3 4 5 6
3. main
Function#
int main(void)
{
int x[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
print_2D_array(x);
}
Purpose: This is the entry point of the program. It initializes a 2D array and calls the
print_2D_array
function to print its elements.
Key Points:#
Array Declaration and Initialization:
int x[2][3] = { {1, 2, 3}, {4, 5, 6} };
A 2D array
x
with 2 rows and 3 columns is declared and initialized.The array is stored in memory as:
Row 0: 1 2 3 Row 1: 4 5 6
Function Call:
print_2D_array(x);
The array
x
is passed to theprint_2D_array
function.In C, when an array is passed to a function, it is passed by reference (as a pointer to the first element). The function can access and process the array elements directly.
4. Program Output#
When the program is executed, the output is:
1 2 3
4 5 6
The
print_2D_array
function prints each row of the array on a new line.
5. Key Concepts Demonstrated#
a. Passing Multidimensional Arrays to Functions#
In C, when a 2D array is passed to a function, the function receives a pointer to the first element of the array.
The dimensions of the array (e.g.,
[2][3]
) must be specified in the function signature to correctly access the elements.
b. Nested Loops for 2D Arrays#
The outer loop iterates over rows, and the inner loop iterates over columns to access each element of the 2D array.
c. Memory Layout of 2D Arrays#
A 2D array in C is stored in row-major order in memory:
All elements of the first row are stored consecutively, followed by all elements of the second row, and so on.
6. How the Code Works Step-by-Step#
The
main
function initializes a 2D arrayx
with the values:1 2 3 4 5 6
The
print_2D_array
function is called withx
as an argument.Inside the
print_2D_array
function:The outer loop iterates over the rows (
row = 0
androw = 1
).For each row, the inner loop iterates over the columns (
col = 0
,col = 1
, andcol = 2
).Each element is printed using
printf
.After printing all elements in a row, a newline is printed to move to the next row.
The program outputs the array elements in a formatted way.
7. Why the Dimensions [2][3]
Are Required#
The compiler needs to know the number of columns in the array (e.g.,
3
in this case) to correctly calculate the memory offset for each element.Without specifying the number of columns, the compiler cannot determine where the next row starts.
This program is a simple and effective demonstration of how to pass and process multidimensional arrays in C.
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_4_passing_multidimensional_arrays_to_functions section_6_6_4_passing_multidimensional_arrays_to_functions.c")
exec_status = os.system("./section_6_6_4_passing_multidimensional_arrays_to_functions")
1 2 3
4 5 6