Thread: Help with multi-dimentional arrays in C

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    51

    Help with multi-dimentional arrays in C

    Hey,

    I wanted to print the values of a array from a function by passing the array as well as the number of elements to be read.

    For a single dimensional array, this is how i have written it. It's pretty straight forward. I want to read 5 elements from the 5th element in the array.

    Code:
    #include<stdio.h>
    void display(int array[],int size)
    {
        int i;
        for(i=0;i<size;i++)
        {
            printf("%d ",array[i]);
        }
    }
    void main()
    {
        int array[]= {1,2,3,4,5,6,7,8,9,10};
        display(&array[4],5);
    }

    But, when i try the same for a multi-dimensional array. I am not able to follow the same approach.

    Code:
    #include<stdio.h>
    void display(int array[][10],int size)
    {
        int i,j;
        for(i=0;i<1;i++)
        {
            for(j=0;j<size;j++)
            {
                printf("%d\n",array[i][j]);
            }
        }
    }
    void main()
    {
        int array[2][10]={ {1,2,3,4,5,6,7,8,9,10},
                        {11,12,13,14,15,16,17,18,19,20}
                       };
        display(&array[0][4],5);
    }

    With this code I want to print the five elements from the element present in [0][4].

    But shows an error that
    Code:
    D:\Bennet\Codeblocks C\Learning C\SingleDimentionalArray.c||In function 'main':|
    D:\Bennet\Codeblocks C\Learning C\SingleDimentionalArray.c|18|warning: passing argument 1 of 'display' from incompatible pointer type [enabled by default]|
    D:\Bennet\Codeblocks C\Learning C\SingleDimentionalArray.c|2|note: expected 'int (*)[10]' but argument is of type 'int *'|
    ||=== Build finished: 0 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
    I know when you pass a array as an argument it gets decomposed into a pointer, but with a multi-dimensional array this is not the case.

    But can't figure out a way to achieve this, Can you explain how this works for mult- dimensional array's?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    Surely you've been here long enough to stop using void main
    FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com

    > I know when you pass a array as an argument it gets decomposed into a pointer, but with a multi-dimensional array this is not the case.
    It's a pointer all-right, but it's type is just not what you expect.

    Some examples
    Code:
    #include<stdio.h>
    void display1(int array)
    {
        printf("%d\n",array);
    }
    void display2(int array[],int size)
    {
        for(int j=0;j<size;j++)
        {
            printf("%d\n",array[j]);
        }
    }
    void display3(int *array,int size)
    {
        for(int j=0;j<size;j++)
        {
            printf("%d\n",array[j]);
        }
    }
    void display4(int array[][10],int size)
    {
      for ( int i = 0 ; i < size ; i++ )
        for(int j=0;j<10;j++)
        {
            printf("%d\n",array[i][j]);
        }
    }
    void display5(int (*array)[10],int size)
    {
      for ( int i = 0 ; i < size ; i++ )
        for(int j=0;j<10;j++)
        {
            printf("%d\n",array[i][j]);
        }
    }
    int main()
    {
        int array[2][10]={ {1,2,3,4,5,6,7,8,9,10},
                        {11,12,13,14,15,16,17,18,19,20}
                       };
        display1(array[0][0]);
        display2(&array[0][4],5);
        display3(&array[0][4],5);
        display2(array[0],5);
        display3(array[0],5);
        display4(array,2);
        display5(array,2);
        return 0;
    }
    A function taking an array parameter can always be written in one of two ways, using either array notation or pointer notation.
    As far as the compiler is concerned, they are identical.

    > note: expected 'int (*)[10]' but argument is of type 'int *'|
    See how this matches the parameter type of display5() ?
    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
    Registered User
    Join Date
    Sep 2014
    Posts
    51
    Thanks for the info about void main() and int main(). It'll change the way I think about main() function.

    Anyways the code provided was of real help. I managed to write something similar to access the given number of elements from a 2D array with a mentioned starting point.

    Here I want to read the five elements from the 2D array, starting from the element 15.

    Code:
    #include<stdio.h>
    void display(int *array,int number)
    {
        int i;
        for(i=0;i<number;i++)
        {
            printf("%d\n",*(array+i));
        }
    }
    int main(void)
    {
    int array[2][10]={
                        {1,2,3,4,5,6,7,8,9,10},
                        {11,12,13,14,15,16,17,18,19,20}
                      };
    int  *pointer;
    int i,j;
    for(i=0;i<2;i++)
     {
    for(j=0;j<10;j++)
        {
            if (array[i][j] ==15)
            {
                pointer = &array[i][j];
                display(pointer,5);
                break;
            }
        }
     }
    return 0;
    }
    Is this approach correct? Given that the pointer is passed any changed made inside the function will also be reflected in the main too.

    Update : I realized just now that the function display() I've written now is exactly the same as display3() in the example provided
    Last edited by thebenman; 11-01-2014 at 08:28 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    Just because your parameter is int* array, doesn't mean you can't do array[i] to index it inside your loop - as opposed to saying the cumbersome *(array+i)

    pointer = &array[i][j];
    display(pointer,5);
    Nor does this buy anything over saying
    display(&array[i][j],5);

    Unless you have some other cause to use pointer for something else as well.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing column data in multi-dimentional arrays
    By Darkmentor in forum C Programming
    Replies: 5
    Last Post: 11-30-2012, 11:51 AM
  2. Multi-Arrays
    By KoG Metalgod in forum C Programming
    Replies: 7
    Last Post: 12-06-2006, 09:56 AM
  3. 3 Dimentional string Arrays
    By paulroseby in forum C Programming
    Replies: 3
    Last Post: 11-27-2002, 06:53 AM
  4. Replies: 10
    Last Post: 10-27-2002, 01:45 AM
  5. moving n-dimentional arrays in c++
    By kknla in forum C++ Programming
    Replies: 0
    Last Post: 02-06-2002, 05:15 PM