Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to pass two-dimensional (2D) array to a function in C

4 Answers

0 votes
#include <stdio.h>
#include <stdint.h>

void print_array(uint8_t arr2D[][4]) {
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 4; j++) {
            printf("%d ", arr2D[i][j]);
        }
        printf("\n");
    }
}

int main(void) {
    uint8_t arr2D[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} };

    print_array(arr2D);

    return 0;
}




/*
run:

1 2 3 4
5 6 7 8
9 10 11 12

*/

 



answered Aug 11, 2017 by avibootz
edited Mar 21, 2023 by avibootz
0 votes
#include <stdio.h>

void print(int matrix[][4], int rows, int cols)
{
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%3d", matrix[i][j]);
        }
        printf("\n");
    }
}

int main(int argc, char** argv)
{
    int matrix[][4] = { { 1, 1, 1, 1 }, { 2, 2, 2, 2 }, { 3, 3, 3, 3 } };

    int rows = sizeof matrix / sizeof(matrix[0]);
    int cols = sizeof matrix[0] / sizeof(matrix[0][0]);

    print(matrix, rows, cols);

    return 0;
}



/*
run:

  1  1  1  1
  2  2  2  2
  3  3  3  3

*/

 



answered Aug 11, 2017 by avibootz
edited Mar 21, 2023 by avibootz
0 votes
#include <stdio.h>

#define ROWS 3
#define COLS 2

void print(int arr[][COLS]);

int main()
{
    int array2D[ROWS][COLS] = { {1, 2}, {3, 4}, {5, 6} };

    print(array2D);

    return 0;
}

void print(int arr[][COLS])
{
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++)
            printf("array2D[%d][%d] = %d\n", i, j, arr[i][j]);
        printf("\n");
    }
}




/*
run:

array2D[0][0] = 1
array2D[0][1] = 2

array2D[1][0] = 3
array2D[1][1] = 4

array2D[2][0] = 5
array2D[2][1] = 6

*/

 



answered Aug 30, 2017 by avibootz
edited Mar 21, 2023 by avibootz
0 votes
#include <stdio.h>

#define ROWS 3
#define COLS 2

void print(int(*arr)[COLS]);

int main()
{
    int array2D[ROWS][COLS] = { {1, 2}, {3, 4}, {13, 18} };

    print(array2D);

    return 0;
}

void print(int(*arr)[COLS])
{
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++)
            printf("array2D[%d][%d] = %d\n", i, j, arr[i][j]);
        printf("\n");
    }
}




/*
run:

array2D[0][0] = 1
array2D[0][1] = 2

array2D[1][0] = 3
array2D[1][1] = 4

array2D[2][0] = 13
array2D[2][1] = 18

*/

 



answered Aug 30, 2017 by avibootz
edited Mar 21, 2023 by avibootz

Related questions

1 answer 173 views
1 answer 149 views
1 answer 122 views
1 answer 222 views
4 answers 212 views
1 answer 200 views
1 answer 122 views
...