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 a pointer to 2D array to a function in C

4 Answers

0 votes
#include <stdio.h>

void print_array(int (*ptr)[5], int cols) {
    int* p = (int*)ptr;

    for (int i = 0; i < (cols * 4); i++) {
        printf("%d ", *p++);
    }
}

int main(void)
{
    int arr[4][5] = { { 0 },
                      { 5, 9, 3, 6, 2 },
                      { [2] = 99, 88 } };

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

    print_array(arr, cols);

    return 0;
}



/*
run:

0 0 0 0 0 5 9 3 6 2 0 0 99 88 0 0 0 0 0 0

*/

 



answered May 7, 2023 by avibootz
edited May 7, 2023 by avibootz
0 votes
#include <stdio.h>

void print_array(int (*ptr)[5], int cols) {
    int* p = (int*)ptr;

    for (int i = 0; i < cols; i++) {
        printf("%d ", *p++);
    }
}

int main(void)
{
    int arr[4][5] = { { 5, 9, 3, 6, 8 },
                      { 0 },
                      { [2] = 99, 88 } };

    int(*p)[5];

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

    p = arr;

    print_array(p, cols);

    return 0;
}



/*
run:

5 9 3 6 8

*/

 



answered May 7, 2023 by avibootz
edited May 7, 2023 by avibootz
0 votes
#include <stdio.h>

void print_array(int (*ptr)[5], int cols) {
    int* p = (int*)ptr;

    for (int i = 0; i < cols; i++) {
        printf("%d ", *p++);
    }
}

int main(void)
{
    int arr[4][5] = { { 5, 9, 3, 6, 8 },
                      { 0 },
                      { [2] = 99, 88 } };

    int(*p)[5];

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

    p = arr;

    for (int i = 0; i < rows; i++) {
        print_array(p++, cols);
        printf("\n");
    }

    return 0;
}



/*
run:

5 9 3 6 8
0 0 0 0 0
0 0 99 88 0
0 0 0 0 0

*/

 



answered May 7, 2023 by avibootz
edited May 7, 2023 by avibootz
0 votes
#include <stdio.h>

void print_array(int (*ptr)[5], int rows, int cols) {
    int* p = (int*)ptr;

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%d ", *(p + i * cols + j));
        }
        printf("\n");
    }
}

int main(void)
{
    int arr[4][5] = { { 5, 9, 3, 6, 8 },
                      { 0 },
                      { [2] = 99, 88 } };

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

    print_array(arr, rows, cols);

    return 0;
}



/*
run:

5 9 3 6 8
0 0 0 0 0
0 0 99 88 0
0 0 0 0 0

*/

 



answered May 7, 2023 by avibootz

Related questions

1 answer 200 views
1 answer 118 views
1 answer 87 views
1 answer 103 views
1 answer 118 views
2 answers 127 views
1 answer 108 views
...