How to pass a 2D array to a pointer in C

09 Dec 2022 Balmiki Mandal 0 C Programming

Mastering 2D Array Pointer Passing in C: Step-by-Step Tutorial

There are two ways to pass a 2D array to a pointer in C:

  • Passing the entire array as a pointer: This is the simplest way to pass a 2D array to a pointer. To do this, simply declare the function parameter as a pointer to the type of the array. For example, to pass a 2D array of integers, you would declare the function parameter as int **array.

  • Passing the base address of the array as a pointer: This is a more efficient way to pass a 2D array to a pointer, but it is also more complex. To do this, you need to cast the name of the array to a pointer to the type of the array. For example, to pass a 2D array of integers, you would cast the name of the array to a pointer to an array of integers, like this: (int (*)[2])array.

How to catch a 2D array passed to a pointer in C

Once you have passed a 2D array to a function as a pointer, you can catch it in the function by declaring the function parameter as a pointer to the type of the array. For example, to catch a 2D array of integers, you would declare the function parameter as int **array.

How to print a 2D array passed to a pointer in C

To print a 2D array that has been passed to a function as a pointer, you can use a nested for loop to iterate over the array and print each element. For example, to print a 2D array of integers, you would use the following code:

C Programming
void print_2d_array(int **array, int rows, int cols) {
  for (int i = 0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {
      printf("%d ", array[i][j]);
    }
    printf("\n");
  }
}

Example code

How to pass a 2D array to a pointer, catch it in the function, and print it:

C Programming
#include<stdio.h>
void print_2d_array(int **array, int rows, int cols) {
  for (int i = 0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {
      printf("%d ", array[i][j]);
    }
    printf("\n");
  }
}

int main() {
  int array[2][2] = {{1, 2}, {3, 4}};

  print_2d_array(array, 2, 2);

  return 0;
}

Output:

1 2
3 4

 

Further Reading:


2D Dimensional Array in c programming

Write a C Program to reverse 2D array Elements

Integer 2D array Passing to function in c

String 2D array to a Function in c

Write a C program to short 2D array using bubble sort

c program to allocate dynamic memory for 2D array integers

Using Arrays of Pointers to Represent Two-Dimensional Arrays

How to Calculate of sub element of 2D array and How to print it in c

Write a C Program to scan 5 Elements and print on the screen using 2D array

Assingement of 2D array in c

Enroll Now:

C-Programming From Scratch to Advanced 2023-2024] "Start Supercharging Your Productivity!"

Contact Us:

  • For any inquiries, please email us at [[email protected]].
  • Follow us on insta  [ electro4u_offical_ ] for updates and tips.

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.