M
masood.iqbal
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 = (int *)malloc(MAXCOLS*sizeof(int));
/* C++: tbl = 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;
}
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 = (int *)malloc(MAXCOLS*sizeof(int));
/* C++: tbl = 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;
}