0
int **p = NULL;

p = (int **) malloc(sizeof (int *) * 3);
for (int i = 0; i < 3; i++)
    p[i] = (int *) malloc(sizeof (int) * 4);    

In this code a double pointer is used as 2D array. My questions are

  1. if i want to insert 1 at every position.

  2. if i want to insert value 2 at specific location

How can i do this?

progmatico
  • 4,714
  • 1
  • 16
  • 27

1 Answers1

2

Just using index operator as 2D array.

if you want to insert 1 at every position.

for (int i = 0; i < 3; i++)
{
    for (int j = 0; j < 4; j++)
        p[i][j] = 1;
}

if you want to insert value 2 at specific location at row i, column j

p[i][j] = 2;
Loc Tran
  • 1,170
  • 7
  • 15