# Thread: Overwriting a row of a dynamic 2D array in C

1. ## Overwriting a row of a dynamic 2D array in C

Hello! I have been trying to come up with a code that overwrites a row of a dynamic 2D array/matrix for quite some time, and when I thought I managed to do it turns out it works for `number of rows>number of columns`type of a matrix only. I am fairly new to this and can not seem to find an error .

Code:
```printf("Enter a number of a row you wish to delete: ");
scanf_s("%d", &k);
if (k >= 0 && k < vr) { //vr is number or rows
for (i = 0; i < kol; i++) { //kol -nmbr of columns
unsigned int *tmp;
for (j = k + 1; j < vr; j++) a[j - 1][i] = a[j][i];
tmp = realloc(a[i], (vr - 1) * sizeof(unsigned int));
if (tmp == NULL) {
printf("ERROR\n");
exit(1);
}
else a[i] = tmp;
}
vr--;```

2. How is a declared in the first place?

If you're starting with
unsigned int **a = malloc( nRows * sizeof(*a));

Where each row is
a[i] = malloc( nCols * sizeof(*a[i]));

To remove a row, it's simply
free(a[i]);
for (j = i+1; j < nRows ; j++) a[j-1] = a[j];

After that, you can use realloc to make a itself shorter, and decrement nRows.

> //vr is number or rows
So change the variable to be something actually readable, then you don't need meaningless comments.