Idiot question about multidimensional arrays

This is a discussion on Idiot question about multidimensional arrays within the C Programming forums, part of the General Programming Boards category; Definition of two-dimensional array: data_type array_name[row_size][column_size]; This: Code: int table[2][3] = { {0,0,0},{1,1,1} }; works. Why? This should mean a ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    111

    Idiot question about multidimensional arrays

    Definition of two-dimensional array:

    data_type array_name[row_size][column_size];

    This:
    Code:
    int table[2][3] = { {0,0,0},{1,1,1} };
    works. Why? This should mean a size 2 row and a size 3 column, shouldn't it?

    Doing this:
    Code:
    int table[2][2] = { {0,0,0},{1,1,1} };
    or this:
    Code:
    int table[2] = {0,0,0};
    does not work. What's the obvious answer here? Why is a size of 3 for rows written as 2 in the 2-dimensional array?

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Arrays start their index at 0, so for an array of three elements in would be: int table[2]; with the individual elements table[0], table[1], and table[2].

    Take a look at the tutorial
    Last edited by AndrewHunter; 06-06-2011 at 12:00 PM.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well, your subsets are your rows. So in your first example you have two rows, three columns each. Each subset is a row.

    In your second example you are providing two rows of three elements in a 2D array that has 2 columns only.

    In your last example you have a 1D array of 2 elements and you are initializing it to contain 3.
    Last edited by claudiu; 06-06-2011 at 12:31 PM.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by AndrewHunter View Post
    Arrays start their index at 0, so for an array of three elements in would be: int table[2]; with the individual elements table[0], table[1], and table[2].

    Take a look at the tutorial
    Wrong. When you allocate memory you get as many as you ask for. So int table[2] is actually an array containing 2 elements [0] and [1].
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Oh right, sorry I don't know what I was thinking. Thanks claudiu.

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    I know that indexes start at zero, of course.

    Ah, dagn - I get it.

    Code:
    int table[2][3] = { {0,0,0},{1,1,1} };
    000 is row one, 111 is row two, of course.

    Thanks :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers and multidimensional arrays question
    By d2rover in forum C Programming
    Replies: 10
    Last Post: 11-07-2010, 09:43 AM
  2. Question on dynamic multidimensional arrays
    By TheLegend219 in forum C++ Programming
    Replies: 2
    Last Post: 09-28-2010, 10:30 AM
  3. A dumb question from an idiot
    By imanidiot in forum C Programming
    Replies: 7
    Last Post: 04-11-2006, 07:03 PM
  4. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  5. Question about multidimensional arrays
    By kippwinger in forum C++ Programming
    Replies: 8
    Last Post: 06-22-2003, 12:57 PM

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