M
masood.iqbal
I am having lots of trouble getting a simple program that initializs a
dynamically allocated 2D array to work. My 2D array is not getting
initialized properly, and additionally I am getting a "Null pointer
assignment" error. Kindly help.
Also, eventually I intend to move this logic to a separate function.
For that, I believe, that I will need to pass a
pointer-to-pointer-to-pointer type as an arguent. Please confirm.
[My apologies to C purists --- I am using the new operator instead of
malloc/calloc since I find its usage more intuitive]
Masood
/************************************************************************/
#include <stdio.h>
#define ROWS 3
#define COLUMNS 5
main()
{
int **tbl;
size_t rows = ROWS;
size_t cols = COLUMNS;
int startVal = 2;
tbl = new (int**)[cols];
for(size_t i = 0; i < rows; i++)
tbl = new (int *)[rows];
for(size_t i1 = 0; i1 < rows; i1++)
for(size_t j1 = 0; j1 < cols; j1++)
tbl[i1][j1] = startVal++;
for(size_t i2 = 0; i2 < rows; i2++)
for(size_t j2 = 0; j2 < cols; j2++)
printf("Row: %d, Col: %d => %d\n",
i2, j2, tbl[i2][j2]);
return 0;
}
dynamically allocated 2D array to work. My 2D array is not getting
initialized properly, and additionally I am getting a "Null pointer
assignment" error. Kindly help.
Also, eventually I intend to move this logic to a separate function.
For that, I believe, that I will need to pass a
pointer-to-pointer-to-pointer type as an arguent. Please confirm.
[My apologies to C purists --- I am using the new operator instead of
malloc/calloc since I find its usage more intuitive]
Masood
/************************************************************************/
#include <stdio.h>
#define ROWS 3
#define COLUMNS 5
main()
{
int **tbl;
size_t rows = ROWS;
size_t cols = COLUMNS;
int startVal = 2;
tbl = new (int**)[cols];
for(size_t i = 0; i < rows; i++)
tbl = new (int *)[rows];
for(size_t i1 = 0; i1 < rows; i1++)
for(size_t j1 = 0; j1 < cols; j1++)
tbl[i1][j1] = startVal++;
for(size_t i2 = 0; i2 < rows; i2++)
for(size_t j2 = 0; j2 < cols; j2++)
printf("Row: %d, Col: %d => %d\n",
i2, j2, tbl[i2][j2]);
return 0;
}