Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Passing 2D array as parameter
2.1.
As an array
2.2.
As a pointer
3.
FAQs
3.1.
What is C language?
3.2.
How to declare a 2D array in C?
3.3.
How are 2D arrays stored in C?
3.4.
How do you pass a 2D array as an argument?
3.5.
How many dimensions does a 2D array have?
4.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

How to pass a 2D array as a parameter in C?

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

C is a procedure-oriented programming language created by Dennis Ritchie in 1972. It provides us with a feature called - “functions” to separate code from the main function. Functions in C can help us reuse the code instead of writing the lengthy and similar codes frequently for the same functionality. We can call the function any number of times and from anywhere to access the code inside it. We pass variables as arguments to the functions to access them inside the functions. 

The variables inside the invoked function are called actual parameters, and the arguments in the function header are called formal parameters. Let’s learn how to pass a 2D array as a parameter to functions in C.

Also See: procedure call in compiler design, C Static Function

Passing 2D array as parameter

We can pass an entire 2D Array as a parameter to a function and access it inside the function. There are two ways to pass a 2D array as a parameter

  • As an array
  • As a pointer

As an array

A 2D array can be passed using many methods. One of the common methods is passing it as an array with dimensions, but using this method can increase the time complexity of the code. The time complexity to pass a 2D array as an argument is O(N^2). We can pass the array as a parameter, as shown below.

#include<stdio.h>

void assignValues(int m, int n, int arr[m][n]) {
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            arr[i][j] = i + j;
        }
}
for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            printf("%3d", arr[i][j]);
        }
        printf("\n");
    }
}
 
// main function
int main(void) {
    int m = 5, n = 5;
    int arr[m][n];
    assignValues(m, n, arr); 
    return 0;
}


Output :

 0  1  2  3  4

 1  2  3  4  5

 2  3  4  5  6

 3  4  5  6  7

 4  5  6  7  8


We initialized the dimensions(row and column size) of the array statically in this code. We invoked a function assignValues by passing the row, columns size, and the entire 2D array as parameters. Then we assign the value to the array variables and print the array.

Click on the following link to read further: Features of C Language and  Short int in C Programming

As a pointer

A 2D array can be passed using many methods. One of the common methods is passing it as a pointer. Pointers store the address of the variable, so changing the value of a variable from anywhere will also change the actual value. We can create an array of pointers and dynamically allocate the memory as shown below:

#include<stdio.h>

void assign(int** arr, int a, int b) {
    for (int i = 0; i < a; i++) {
        for (int j = 0; j < b; j++) {
            arr[i][j] = i + j;
        }
    }
 for (int i = 0; i < a; i++) {
        for (int j = 0; j < b; j++) {
            printf("%3d", arr[i][j]);
        }
        printf("\n");
    }
}
 
//main function
int main(void) {
    int a = 5;
    int b = 5; 

    int **arr = (int **)malloc(a * sizeof(int *));
    for (int r = 0; r < a; r++) {
        arr[r] = (int *)malloc(b * sizeof(int));
    }
 
    assign(arr, a, b);
 
    // deallocate memory
    for (int i = 0; i < a; i++) {
        free(arr[i]);
    }
    free(arr);
    return 0;
}


Output:

0 1 2 3 4

1 2 3 4 5

2 3 4 5 6

3 4 5 6 7

4 5 6 7 8

We initialized the dimensions(row and column size) of the array in the code and declared the array dynamically as a pointer, so the size of the array could be flexible and modified. We invoked a function assignValues by passing the row, columns size, and the array pointer as arguments. Then we assign the value to the array variables and print the array. We can also print the array from the main function without returning any values to the main, as the value gets changed according to the address.

You can also read about dynamic array in c, And Tribonacci Series

Must Read what is storage class in c

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

FAQs

What is C language?

C is a high-level and procedure-oriented programming language ideal for developing firmware applications. It was created by Dennis Ritchie in 1972.

How to declare a 2D array in C?

We can declare a 2D array as an array by following the syntax: dataType array_name[row_size][column_size] or we can also declare it using the pointers by following the syntax:  datatype **arrayName = (datatype **)malloc(size * sizeof(datatype *))

How are 2D arrays stored in C?

The 2D arrays are stored in the computer memory. The address of the first byte of the array memory is considered the memory location of the entire 2D array.

How do you pass a 2D array as an argument?

We can pass the 2D array as an argument to the function in C in two ways; by passing the entire array as an argument, or passing the array as a dynamic pointer to the function. 

How many dimensions does a 2D array have?

The 2D array has two dimensions; row and column. The array is represented by printing the values of respective rows and columns according to the mentioned dimensions. 

Key Takeaways

We have discussed the concept of passing the 2D array as a parameter in C in this article with code. You can now use the 2D array in your code and perform operations on them. 

Hey Ninjas! We hope this blog helped you understand the concept of passing a 2D array as a parameter in C. Please check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems. Please upvote our blog to help the other ninjas grow.

Recommended Readings: 

Recommended problems -

 

Happy Coding!

Previous article
Dynamically Allocate a 2D Array in C
Next article
Accessing an array out of bounds in C/C++
Live masterclass