Is the syntax for multi-dimensional arrays counter-intuitive?
masood.iqbal_at_lycos.com
Date: 02/03/05
- Next message: masood.iqbal_at_lycos.com: "Pointer-to-pointer-to-pointer question"
- Previous message: David Hopwood: "Re: Bit-fields and integral promotion"
- Next in thread: A. Bolmarcich: "Re: Is the syntax for multi-dimensional arrays counter-intuitive?"
- Reply: A. Bolmarcich: "Re: Is the syntax for multi-dimensional arrays counter-intuitive?"
- Reply: Keith Thompson: "Re: Is the syntax for multi-dimensional arrays counter-intuitive?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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;
}
- Next message: masood.iqbal_at_lycos.com: "Pointer-to-pointer-to-pointer question"
- Previous message: David Hopwood: "Re: Bit-fields and integral promotion"
- Next in thread: A. Bolmarcich: "Re: Is the syntax for multi-dimensional arrays counter-intuitive?"
- Reply: A. Bolmarcich: "Re: Is the syntax for multi-dimensional arrays counter-intuitive?"
- Reply: Keith Thompson: "Re: Is the syntax for multi-dimensional arrays counter-intuitive?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|