Thread: array 2 dimension using pointer and function in c

  1. #1
    Registered User
    Join Date
    Dec 2020
    Posts
    5

    array 2 dimension using pointer and function in c

    i stuck with my code to change this code by using a pointer. mybe someone want to help me fixed it.

    this my code :
    Code:
    #include <stdio.h>
    #define size 100
    
    
    int array2d(int x[size][size], int a, int b)
    {
        int i, j;
        for (i = 0; i < a; i++)
        {
            for (j = 0; j < b; j++)
            {
                printf("%d ", x[i][j]);
            }
            printf("\n");
        }
    }
    int main()
    {
        int a, b;
        int x[size][size];
    
    
        printf("enter the size of array (row) & (column) : ");
        scanf("%d %d", &a, &b);
    
    
        printf("enter the number : ");
    
    
        for (int i = 0; i < a; i++) //reading input
        {
            for (int j = 0; j < b; j++)
            {
                scanf("%d", &x[i][j]);
            }
        }
    
    
        array2d(x, a, b);
    
    
        return 0;
    }
    please help me

    this code input :
    2 2

    and
    1 2 3 4

    should be an output :
    1 2
    3 4
    Last edited by riandrp; 12-05-2020 at 10:24 AM.

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    167
    It does:

    Code:
    $ ./main 
    enter the size of array (row) & (column) : 2 2
    enter the number : 1 2 3 4
    1 2 
    3 4 
    $
    Do you still have a problem?

  3. #3
    Registered User
    Join Date
    Dec 2020
    Posts
    5
    yes i have problem to add a pointer in there

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,485
    What do you mean - add pointer?

    You can make a 1-line change of
    Code:
    int array2d(int (*x)[size], int a, int b)
    Does that count?

    If you were looking for
    Code:
    int array2d(int **x, int a, int b)
    then that's rather more work in main.

    Code:
    int **x;
    x = malloc( a * sizeof(*x));
    for ( i = 0 ; i < a ; i++ ) x[i] = malloc( b*sizeof(*x[i]));
    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.

  5. #5
    Registered User
    Join Date
    Dec 2020
    Posts
    5
    can you help me to write a complete code using a poniter from the code that i have created

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,485
    Erm, that's all you needed to do.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define size 100
    
    
    int array2d(int **x, int a, int b)
    {
        int i, j;
        for (i = 0; i < a; i++)
        {
            for (j = 0; j < b; j++)
            {
                printf("%d ", x[i][j]);
            }
            printf("\n");
        }
    }
    int main()
    {
        int a, b;
    //    int x[size][size];
        int **x;
    
    
        printf("enter the size of array (row) & (column) : ");
        scanf("%d %d", &a, &b);
    
        x = malloc( a * sizeof(*x));
        for ( int i = 0 ; i < a ; i++ ) x[i] = malloc( b*sizeof(*x[i]));
    
        printf("enter the number : ");
    
    
        for (int i = 0; i < a; i++) //reading input
        {
            for (int j = 0; j < b; j++)
            {
                scanf("%d", &x[i][j]);
            }
        }
    
    
        array2d(x, a, b);
    
    
        return 0;
    }
    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.

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    482
    Most beginners' C books introduce 2D arrays iimmediately after 1D arrays.

    This is a mistake. Multidimensional arrays were historically not implemented well in C, and should be thought of as an advanced feature. The situation is a bit better with the latest versions of C, but these are not supported everywhere.

    What most C programmers do is to pass buffers for naturally 2D data such as images. The buffer is then accessed using the formula buffer[y*width+x]. It's not less efficient as the compiler will do that underlying calculation anyway. And it makes it easy to have arrays with dimensions determined at runtime.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  8. #8
    Registered User
    Join Date
    Dec 2020
    Posts
    5
    thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-11-2011, 02:49 AM
  2. Double Pointer & Two Dimension Array ....
    By gokulvs in forum C Programming
    Replies: 7
    Last Post: 09-09-2009, 11:04 PM
  3. Passing a pointer to two-dimension array in a function
    By E_I_S in forum C++ Programming
    Replies: 11
    Last Post: 06-19-2008, 09:57 AM
  4. pass the pointer of a two dimension array to a function
    By SoFarAway in forum C Programming
    Replies: 8
    Last Post: 04-13-2005, 05:43 AM
  5. Passing a 2 dimension array to a function
    By Chuck in forum C++ Programming
    Replies: 8
    Last Post: 12-09-2002, 09:42 AM

Tags for this Thread