- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Passing Arrays to Functions
![]() Share with a Friend |
C Programming - C Passing Arrays to Functions
Passing Arrays to Functions in C
In C, arrays can be passed to functions either by reference or pointer. Since arrays are always passed by reference in C, when you pass an array to a function, you're passing the address of its first element. This means that changes made to the array inside the function affect the original array.
- Passing Arrays by Reference (Using Array Name)
When you pass an array to a function, you're essentially passing the base address (the address of the first element of the array) to the function. This means the function can access and modify the original array.
Syntax:
C
return_type function_name(data_type array_name[], int size);
The size of the array is not passed automatically, so you should either define a separate argument to specify the size of the array or use a sentinel value to indicate the end of the array.
Example:
C
#include <stdio.h>
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]); // Calculate the size of the array
printArray(arr, size); // Passing the array to the function
return 0;
}
Explanation:
- The arr[] in the function parameter means that the array is being passed by reference. The function accesses and prints the original array.
- The size of the array is calculated using sizeof(arr) / sizeof(arr[0]) to determine the number of elements.
Output:
1 2 3 4 5
- Passing Arrays Using Pointers
Arrays are internally treated as pointers in C, and you can explicitly pass the array as a pointer to the function. This is equivalent to passing the array by reference, but with the pointer explicitly used to access the array.
Syntax:
C
return_type function_name(data_type *array_name, int size);
Example:
C
#include <stdio.h>
void modifyArray(int *arr, int size) {
for (int i = 0; i < size; i++) {
arr[i] = arr[i] * 2; // Modify each element by multiplying it by 2
}
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]); // Calculate the size of the array
modifyArray(arr, size); // Pass the array to the function
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]); // Print the modified array
}
return 0;
}
Explanation:
- The function modifyArray receives a pointer to the array and modifies the array by multiplying each element by 2.
- The array is passed directly to the function, and the changes affect the original array because it's passed by reference.
Output:
2 4 6 8 10
- Passing Multidimensional Arrays to Functions
Multidimensional arrays can also be passed to functions in a similar way, but you need to specify the size of each dimension except the first one. This is because the compiler needs to know how to access elements beyond the first dimension.
Syntax for 2D Array:
C
return_type function_name(data_type array_name[][size2], int size1, int size2);
Example:
C
#include <stdio.h>
void print2DArray(int arr[][3], int rows) { // 3 is the number of columns
for (int i = 0; i < rows; i++) {
for (int j = 0; j < 3; j++) { // 3 is the number of columns
printf("%d ", arr[i][j]);
}
printf("\n");
}
}
int main() {
int arr[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
print2DArray(arr, 2); // Passing 2D array to the function
return 0;
}
Explanation:
- The arr[][3] parameter in the function means that the number of columns (3) is known, and the rows can be passed as an argument.
- The function prints each element of the 2D array.
Output:
1 2 3
4 5 6
- Important Points
- Size of Arrays: When passing arrays to functions, the size of the array is not passed automatically. If you need to know the size of the array, you must either pass the size explicitly or use some sentinel value (e.g., -1, 0, etc.).
- Passing Entire Arrays: In C, when an array is passed to a function, what is actually passed is a pointer to the first element of the array. Hence, modifying the array inside the function will affect the original array.
- Multidimensional Arrays: When passing a multidimensional array, the size of the second and subsequent dimensions must be provided, as the array is stored in a contiguous block of memory.
Summary
- Arrays in C are always passed by reference, which means changes made to the array inside a function will affect the original array.
- You can pass arrays either using the array name (which implicitly means passing a pointer) or explicitly as a pointer.
- When passing a multidimensional array, the size of all but the first dimension must be specified for proper access inside the function.
