Thread: Passing a 2d array to a function, using the pointer to a 2D array

  1. #1
    Registered User
    Join Date
    Jun 2021
    Posts
    1

    Passing a 2d array to a function, using the pointer to a 2D array

    Hi,

    I am trying to make a program, a read from the keyboard a 2d array and print his elements.
    It works, but i am not so sure is very good, because when i compile, take a while and it finish with a delay.
    When I make debug I obtain a segmentation fault. So, I need some help.
    Thank you in advance.

    Code:
    #include <stdio.h>​
    #define ARRAY_ROW 3
    #define ARRAY_COL 3​
    
    void WriteArray3(int(*piData)[ARRAY_ROW][ARRAY_COL])
    {
        int iRow = 0;
        int iCol = 0;​
    
        for(iRow = 0; iRow<ARRAY_ROW; iRow++)
        {
            for(iCol = 0; iCol<ARRAY_COL; iCol++)
            {
                printf("\n Element[%d][%d] = ", iRow, iCol);
                scanf("%d", &piData[iRow][iCol]);
            }
            printf("\n");
        }​
    }​
    
    void ReadArray3(int(*piData)[ARRAY_ROW][ARRAY_COL])
    {
        int iRow = 0;
        int iCol = 0;​
    
        printf("\n");
        for(iRow = 0; iRow<ARRAY_ROW; iRow++)
        {
            for(iCol = 0; iCol<ARRAY_COL; iCol++)
            {
                printf(" %d ", (*piData[iRow][iCol]));
            }
            printf("\n\n");
        }​
    }​
    
    int main()
    {
        int aiData[ARRAY_ROW][ARRAY_COL];​
    
        WriteArray3(&aiData);
        ReadArray3(&aiData);​
    
        return 0;​
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338

    Post

    Code:
    #include <stdio.h>
    #define ARRAY_ROW 3
    #define ARRAY_COL 3
    
    void WriteArray(int piData[ARRAY_ROW][ARRAY_COL]) {
        for(int iRow = 0; iRow<ARRAY_ROW; iRow++) {
            for(int iCol = 0; iCol<ARRAY_COL; iCol++) {            
                printf("\n Element[%d][%d] = ", iRow, iCol);
                int entered = 0; scanf("%d", &entered);
                piData[iRow][iCol] = entered;
            }
            printf("\n");
        }
    }
    
    void ReadArray(int piData[ARRAY_ROW][ARRAY_COL]) {
        for(int iRow = 0; iRow<ARRAY_ROW; iRow++) {
            for(int iCol = 0; iCol<ARRAY_COL; iCol++) {
                printf( " %d ", piData[iRow][iCol] );
            }
            printf( "\n" );
        }
        printf( "\n\n" );
    }
    
    int main() {
    
        int aiData[ARRAY_ROW][ARRAY_COL];
    
        WriteArray( aiData );
        ReadArray( aiData );
        
        return 0;
    }
    "without goto we would be wtf'd"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing a pointer to a 2d array to a function
    By cooper1200 in forum C Programming
    Replies: 3
    Last Post: 04-28-2019, 04:30 PM
  2. Replies: 4
    Last Post: 09-08-2013, 06:17 PM
  3. Passing two dimensional array to function using pointer
    By Burns111 in forum C Programming
    Replies: 3
    Last Post: 11-27-2012, 10:55 AM
  4. Replies: 11
    Last Post: 12-30-2009, 04:04 PM
  5. Passing pointer to array to a function
    By Tojam in forum C Programming
    Replies: 1
    Last Post: 10-09-2002, 09:24 PM

Tags for this Thread