Is the syntax for multi-dimensional arrays counter-intuitive?

masood.iqbal_at_lycos.com
Date: 02/03/05


Date: 2 Feb 2005 20:19:38 -0800

All this time I was under the illusion that I understand the concept of
multi-dimensional arrays well ---- however the following code snippet
belies my understanding.

I had assumed all along that a declaration like:
int intArray[3][5]
means that there is an array of pointers to int with a size of 5, each
of whose elements is an array of int with a size of 3.

This definition seemed intuitive to me since a declaration like
int intVar[5]
means that there is an array of integers with a size of 5.

In my "intuitive" thinking, to dynamically create a 2D array with 3
rows and 5 columns, we would do the following:

Allocate memory for 5 pointers to int (i.e. pointer array)
For each entry in the pointer array
Do
Allocate memory for 3 ints
Done

My code had a bug, which was fixed by switching my logic to something
like this (as can be seen from the coding example):

Allocate memory for 3 pointers to int (i.e. pointer array)
For each entry in the pointer array
Do
Allocate memory for 5 ints
Done

This indicates to me that the correct interpretation of a declaration
like:

int intArray[3][5]

is that there is an array of pointers to int with a size of 3, each of
whose elements is an array of int with a size of 5. Is this a correct
interpretation? This sounds counter intuitive to me (for reasons
mentioned above) but I have reconciled to this interpretation. I will
jump off the cliff if I am wrong again!

Masood

/*************************************************/
#include <stdio.h>
#include <stdlib.h>

#define MAXROWS 3
#define MAXCOLS 5

main()
{
int startVal = 5;

int **tbl;

tbl = (int **)malloc(MAXROWS*sizeof(int*));
/* C++ : tbl = new (int*)[MAXROWS]; */

for(size_t i = 0; i < MAXCOLS; i++)
tbl[i] = (int *)malloc(MAXCOLS*sizeof(int));
/* C++: tbl[i] = new (int)[MAXCOLS]; */

for(size_t row = 0; row < MAXROWS; row++)
for(size_t col = 0; col < MAXCOLS; col++)
tbl[row][col] = startVal++;

for(size_t row = 0; row < MAXROWS; row++)
for(size_t col = 0; col < MAXCOLS; col++)
printf("Row: %d, Col: %d => %d\n",
row, col, tbl[row][col]);
  return 0;
}



Relevant Pages

  • (patch for Bash) regex case statement
    ... Following up on my previous patch for regex conditional tests, ... /* Return an array of strings; ... int dollarflag, zeropad, compareflag; ... SHELL_VAR *var; ...
    (comp.unix.shell)
  • Re: Strategy or Iterator?
    ... It would be possible to write a class that returns the variations ... GNU General Public License for more details. ... protected CombinatoricOperator(Telements, int r) { ... An integer array backing up the original one to keep track of the ...
    (comp.lang.java.programmer)
  • (patch for Bash) regex conditional tests
    ... 'regex' are returned in array variable SUBMATCH. ... Skipping of positional parameters, array elements, string ... int dollarflag, zeropad, compareflag; ... SHELL_VAR *var; ...
    (comp.unix.shell)
  • Re: 2D array of structures
    ... necessary for later reuse in array ... // it's time to allocate memory on the fly, ... // Optional exit, ...
    (comp.lang.c)
  • Re: attempting an actual game...
    ... >>> and inflexible by the absurd decision to use a bit array for square ... as then one has 8 bits in which to store a color and a few flags ... Using a 2D int (or, ... > Change direction and you may eventually complete a game. ...
    (comp.games.development.programming.misc)