2 dimensional array

This is a discussion on 2 dimensional array within the C Programming forums, part of the General Programming Boards category; Hey guys , I am trying to self-study C programming during this summer. I was really confused about this 2 ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    12

    2 dimensional array

    Hey guys , I am trying to self-study C programming during this summer.
    I was really confused about this 2 dimensional array thing.
    Hope that I could get some helps here.

    I know how to create one with malloc, and how to access them.(with x[i][j])

    but , the hardest part for me is how to pass it to a function.

    Let us say that My program will malloc a 2 dimensional array based on the input(row,col) given by the user.
    I wish to pass this array to a function and to do something with it.

    What is the best way to do it?

    Thanks ahead for any replies.
    Zhen

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    The exact answer really depends on how you use malloc(). For starters, if you allocated each dimension independently, which requires you to use the most malloc() calls, you end up with a pointer to pointer variable to access the structure, and you would want to pass over the dimension sizes as well.

    Code:
    void dosomething(double **mat, int columnsize, int rowsize);
    ...
    double **mat = malloc(columnsize * sizeof(mat[0]));
    for (i = 0; i < columnsize; i++) {
      mat[i] = malloc(rowsize * sizeof(mat[i][0]));
    }
    ...
    dosomething(mat, columnsize, rowsize);
    Cleverer ways of allocating exist and they all have positives and negatives.
    Last edited by whiteflags; 05-11-2013 at 08:48 PM.

  3. #3
    Registered User hex_dump's Avatar
    Join Date
    Dec 2012
    Posts
    88
    Declare like so
    Code:
    void init_array(double (*the_array)[ELEMENTS], int rows);
    Invoke it like so
    Code:
    init_array(some_array, RECORDS);
    thats the name of the array and the num of rows.

    Then you define it like so
    Code:
    void init_array(double (*the_array)[ELEMENTS], int rows){
    .....
    }
    That's pretty much it.


    Oh just saw the above
    Last edited by hex_dump; 05-11-2013 at 08:52 PM.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    12
    Whiteflags, Thank you for the reply
    This is normally how I malloc 2 dimensional array.

    If you invoke like(dosomething(mat, columnsize, rowsize),
    and you said that this is pointer pointer form,so ,Could you able to access it with array[i][j] or you have to calculate it?

    Thanks

    Also:
    from the text book, it states that i could define like this

    bool isMagic (int square [N][N], int N)
    How should i invoke if the function define like this?

    sorry for all that questions, I just hope that i can get the bottom of it.
    Last edited by zhangz64; 05-11-2013 at 10:52 PM.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    12
    Thank you hex_dump,
    This is how i understand this ,correct me if i am wrong, we first malloc a double pointer which points a list of arrays. then you malloc for each arrays.
    So,should i malloc like this?

    double **mat = malloc(NumberOfrows* sizeof(mat[0]));
    for (i = 0; i < NumberOfrows; i++) {
    mat[i] = malloc(NumberOfCol* sizeof(mat[i][0]));

    Also,when you invoke,
    the "ELEMENTS" in (*the_array)[ELEMENTS] is a variable right?or is just a constant? because i dont see how we could pass the number of columes to the function.
    from i understand, if you define like
    arry[x]
    ,the complier will automatically ignore the x.

    So,Within the function, Would i be able to access it with array[i][j]?or i have to calculate it?

    Thanks

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    Quote Originally Posted by zhangz64 View Post
    If you invoke like(dosomething(mat, columnsize, rowsize),
    and you said that this is pointer pointer form,so ,Could you able to access it with array[i][j] or you have to calculate it?

    Thanks
    Sure. So when you write the dosomething function, an expression like mat[x][y] would access elements.

    Also:
    from the text book, it states that i could define like this

    bool isMagic (int square [N][N], int N)
    How should i invoke if the function define like this?

    sorry for all that questions, I just hope that i can get the bottom of it.
    That example looks like an automatic array to me, so it could be that, and malloc is not involved at all. On the other hand, could have code like:
    Code:
      int (*square)[N] = malloc(N * sizeof(square[0]));
    Which makes something you can pass to that function by name.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,764
    What style of array declaration you use depends on how much you know at compile time.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define ROWS  5
    #define COLS  10
    
    void foo ( int array[ROWS][COLS], int rows, int cols ) {
      // just use array[r][c] here
    }
    void bar ( int (*var_rows)[COLS], int rows, int cols ) {
      // just use var_rows[r][c] here
    }
    void baz ( int **var_rowscols, int rows, int cols ) {
      // just use var_rowscols[r][c] here
    }
    
    int main(){
      int array[ROWS][COLS];        // both known at compile time
      int (*var_rows)[COLS];        // only COLS known at compile time
      int **var_rowscols;           // nothing known at compile time
      int nRows = 20, nCols = 50;   // run-time sizes
    
      var_rows = malloc( nRows * sizeof(*var_rows) );
      
      var_rowscols = malloc( nRows * sizeof(*var_rowscols) );
      for ( int r = 0 ; r < nRows ; r++ ) {
        var_rowscols[r] = malloc( nCols * sizeof(*var_rowscols[r]) );
      }
      
      foo(array,ROWS,COLS);
      bar(var_rows,nRows,COLS);
      baz(var_rowscols,nRows,nCols);
      
      return 0;
    }
    Some things to note:
    1. The parameter declaration is a copy/paste of the variable declaration in main.
    2. [r][c] notation works regardless of whichever style of 2D array you choose to create.
    3. The actual parameter passed is just the variable name itself - no mysterious & or * needed.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    12
    very clear, Thank you Salem

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    12
    I think i got it .
    Thank you whiteflags.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 24
    Last Post: 11-11-2008, 01:39 PM
  2. Two dimensional array help
    By hrider in forum C Programming
    Replies: 3
    Last Post: 10-21-2008, 06:58 AM
  3. Two dimensional array
    By George2 in forum C Programming
    Replies: 3
    Last Post: 11-10-2007, 05:27 AM
  4. 2 dimensional array
    By chasesg in forum C Programming
    Replies: 2
    Last Post: 10-13-2007, 01:12 PM
  5. Replies: 1
    Last Post: 04-25-2006, 01:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21