Like Tree2Likes
  • 1 Post By Bayint Naung
  • 1 Post By Inanna

Two-dimensional array problem - static vs. dynamic approach

This is a discussion on Two-dimensional array problem - static vs. dynamic approach within the C Programming forums, part of the General Programming Boards category; Hi, the code below ends with EXC_BAD_ACCESS when I run it. I also get a warning message from the compiler ...

  1. #1
    kkk
    kkk is offline
    Registered User
    Join Date
    May 2011
    Posts
    32

    Two-dimensional array problem - static vs. dynamic approach

    Hi,

    the code below ends with EXC_BAD_ACCESS when I run it. I also get a warning message from the compiler telling me that array_s is an incompatible pointer type when calling the fill_array function. Where is the problem? If I write similar code working with one-dimensional array, it functions properly.

    Code:
    #define ROWS 3
    #define COLUMNS 4
    
    void fill_array(int **arr, int rows, int columns)
    {
    	int i, j;
    	for (i = 0; i < rows; i++)
    		for (j = 0; j < columns; j++)
    			arr[i][j] = columns * i + j;
    			 
    }
    
    int main (int argc, const char * argv[])
    {
        int array_s[ROWS][COLUMNS];
        fill_array(array_s, ROWS, COLUMNS);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Question 6.18
    Next time, please read c-faq before asking here.
    Salem likes this.

  3. #3
    Registered User Inanna's Avatar
    Join Date
    May 2011
    Posts
    69
    Where is the problem?
    array_s is an incompatible pointer type to what fill_array expects. The first dimension of an array decays into a pointer, but only the first dimension. array_s becomes int(*)[4] but fill_array expects int**. A simpler rule to remember is a 2D array of T is not compatible with a double pointer to T.
    Salem likes this.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by kkk View Post
    Where is the problem?
    The problem is you not listening to your compiler. Turn up your warning levels, and pay attention to them.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic two dimensional array free problem
    By nickman in forum C Programming
    Replies: 14
    Last Post: 01-13-2011, 12:44 PM
  2. 2-Dimensional Dynamic Array
    By juice_box in forum C Programming
    Replies: 4
    Last Post: 11-30-2010, 06:49 PM
  3. Dynamic 2 Dimensional Array
    By mrb260478 in forum C Programming
    Replies: 23
    Last Post: 06-21-2008, 08:19 AM
  4. Dynamic 2 dimensional array with new?
    By Quantum1024 in forum C++ Programming
    Replies: 10
    Last Post: 11-19-2005, 02:46 PM
  5. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 05:27 PM

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