

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to pass a 2D array as a parameter in C?
A 2-D array can be easily passed as a parameter to a function in C. A program that demonstrates this when both the array dimensions are specified globally is given as follows.
Example
#include <stdio.h> const int R = 4; const int C = 3; void func(int a[R][C]) { int i, j; for (i = 0; i < R; i++) for (j = 0; j < C; j++) a[i][j] += 5; ; } int main() { int a[R][C]; int i, j; for (i = 0; i < R; i++) for (j = 0; j < C; j++) a[i][j] = i+j; printf("Initial 2-D array is:\n"); for (i = 0; i < R; i++) { for (j = 0; j < C; j++) { printf("%d ", a[i][j]); } printf("\n"); } func(a); printf("Modified 2-D array is:\n"); for (i = 0; i < R; i++) { for (j = 0; j < C; j++) { printf("%d ", a[i][j]); } printf("\n"); } return 0; }
Output
The output of the above program is as follows.
Initial 2-D array is: 0 1 2 1 2 3 2 3 4 3 4 5 Modified 2-D array is: 5 6 7 6 7 8 7 8 9 8 9 10
- Related Questions & Answers
- How to pass a function as a parameter in Java
- How to pass a jQuery event as a parameter in a method?
- How to pass a lambda expression as a method parameter in Java?
- How to pass an object as a parameter in JavaScript function?
- How to pass a json object as a parameter to a python function?
- C# Program to pass Parameter to a Thread
- How can I pass a parameter to a setTimeout() callback?
- How to pass entire array as an argument to a function in C language?
- Set tuple as a method parameter in C#
- How to dynamically allocate a 2D array in C?
- How to store a 2d Array in another 2d Array in java?
- Passing a 2D array to a C++ function
- C++ Perform to a 2D FFT Inplace Given a Complex 2D Array
- How to convert a 2D array into 1D array in C#?
- How to create a dynamic 2D array inside a class in C++
Advertisements