Thread: Why do we need to specify column size when writing a 2D array In C??

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    34

    Unhappy Why do we need to specify column size when writing a 2D array In C??

    The following code throws error !

    Code:
    #include <stdio.h>
    
    int main(void)
    {
          int array[][]={{1,2,3},{123,244,232}};
    }

    This works fine!

    Code:
    #include <stdio.h>
    
    int main(void)
    {
          int array[][3]={{1,2,3},{123,244,232}};
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Because only the leftmost index can be arbitrary, all other indices must have a fixed size. In other words, all 2D arrays in C must be rectangular, with the same amount of columns for every row.
    Last edited by GReaper; 11-22-2017 at 08:51 AM.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Nov 2017
    Posts
    34
    Quote Originally Posted by GReaper View Post
    Because only the leftmost index can be arbitrary, all other indices must have a fixed size. In other words, all 2D arrays in C must be rectangular, with the same amount of columns for every row.
    That is what is written in the book. There is a post on stack overflow about this. Here

    Can you please explain it. I can't seem to understand the answer to the question in that post. Please

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    ans 20 explained it rather well.

  5. #5
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    In the example question you posted, the OP is asking about passing an array to a function. This is a little bit different than your plain array definition.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting a 2d array by column
    By antros48 in forum C Programming
    Replies: 4
    Last Post: 10-24-2013, 09:11 AM
  2. Subtracting one column in a 2d array from another?
    By zaihed13 in forum C Programming
    Replies: 6
    Last Post: 10-19-2013, 09:29 AM
  3. Adding a new column in array
    By Tomislav Pti in forum C Programming
    Replies: 3
    Last Post: 11-22-2012, 04:48 AM
  4. Replies: 6
    Last Post: 12-09-2010, 06:27 PM
  5. Writing to a column in a text file
    By olland in forum C Programming
    Replies: 2
    Last Post: 01-21-2002, 06:40 AM

Tags for this Thread