You can use realloc on each row if you need to add a column: That is, if you need to add N columns, you realloc each row by extending each of them by N.
EDIT: You can look at this possible duplicate
Question
I'm allocating memory for a 2d array dynamically using the below code,
int **matrix = (int **) malloc(testVals[m].rows*sizeof(int));
for(int i = 0 ; i < testVals[m].rows ; i++)
matrix[i] = (int *) malloc(testVals[m].columns*sizeof(int));
but I'm in need to add extra rows and columns as per requirements. I'm able to add extra rows like this, where i'm converting the matrix to square matrix
for(i = rows ; i < cols ; i++)
mat[i] = (int *) malloc(cols*sizeof(int));
but how to do that for adding columns?
Solution
You can use realloc on each row if you need to add a column: That is, if you need to add N columns, you realloc each row by extending each of them by N.
EDIT: You can look at this possible duplicate