← Back to C Course | Chapter 6: Arrays & Strings | Lesson 5 of 8

C Arrays & Functions

Passing an array to a function hands over the original row of boxes, so the function can read or change them. You usually pass the size too, because the function does not know it.
Syntax
c
return_type function_name(data_type array_param[], int size) {
  // body
}

function_name(array_name, size);

Passing Arrays to Functions

When an array decays to a pointer at the function boundary, the function receives only the address of the first element -- the brackets in the parameter list like int arr[] are purely cosmetic and behave identically to int *arr.

Example: Passing Arrays to Functions

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
void printFirst(int arr[]) {
	// Print formatted text to the screen
	printf("%d", arr[0]);
}
// Program execution starts in main()
int main() {
	int nums[3] = {10, 20, 30};
	printFirst(nums);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Passing Array Size

This is one of C's most-cited design quirks: sizeof(arr) inside the calling function returns the full array size, but sizeof(arr) inside the called function returns the size of a pointer (8 bytes on most 64-bit systems), so the size must always be passed explicitly.

Example: Passing Array Size

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
void printAll(int arr[], int size) {
	// Loop: repeat while the condition holds
	for (int i = 0; i < size; i++) {
		// Print formatted text to the screen
		printf("%d ", arr[i]);
	}
}
// Program execution starts in main()
int main() {
	int nums[3] = {10, 20, 30};
	printAll(nums, 3);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Modifying Arrays inside Functions

This pass-by-reference-like behavior is different from how C treats other variable types -- passing an int by value protects the original from changes, but arrays are always effectively passed by reference because only their address is copied.

Example: Modifying Arrays inside Functions

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
void doubleValues(int arr[], int size) {
	// Loop: repeat while the condition holds
	for (int i = 0; i < size; i++) {
		arr[i] *= 2;
	}
}
// Program execution starts in main()
int main() {
	int nums[3] = {1, 2, 3};
	doubleValues(nums, 3);
	// Print formatted text to the screen
	printf("%d %d %d", nums[0], nums[1], nums[2]);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Passing Multi-dimensional Arrays

The compiler needs the column count to compute how far to jump in memory between rows (row_size * sizeof(element)), so while arr[][5] is valid as a parameter, arr[][] alone is not enough information for the compiler to index correctly.

Example: Passing Multi-dimensional Arrays

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
void printGrid(int arr[][2], int rows) {
	// Loop: repeat while the condition holds
	for (int i = 0; i < rows; i++) {
		// Print formatted text to the screen
		printf("%d %d ", arr[i][0], arr[i][1]);
	}
}
// Program execution starts in main()
int main() {
	int grid[2][2] = {{1, 2}, {3, 4}};
	printGrid(grid, 2);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Returning Arrays Safely

Declaring the array static inside the function keeps it alive after the function returns, unlike a normal local array which is deallocated from the stack -- the tradeoff is that a static array is shared across all calls to that function, which can cause bugs if you're not careful.

Example: Returning Arrays Safely

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
int* getArray() {
	static int arr[3] = {1, 2, 3};
	// Send arr back to the caller
	return arr;
}
// Program execution starts in main()
int main() {
	int *result = getArray();
	// Print formatted text to the screen
	printf("%d", result[0]);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Expecting sizeof inside the function to give the array length.
  2. Forgetting to pass the array size, so the function reads past the end.
  3. Assuming arrays are copied when passed, when the function works on the original.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.