Ok got the delete_column function coded now but I just have one doubt, here it is:
Code:
/* delete_column: delete column n from table t */
void delete_column(table *t, int n){
int i, j;
--(t->cols);
char ***data = malloc(t->rows * sizeof(char **));
if(data == NULL){
printf("\n\t\tERROR: Out of memory\n");
printf("\n\t\tPress [Enter] to exit...\n");
getchar();
exit(EXIT_FAILURE);
}
for(i = 0; i < t-> rows; ++i){
data[i] = (char **)malloc(t->cols * sizeof(char *));
if(data[i] == NULL){
printf("\n\t\tERROR: Out of memory\n");
printf("\n\t\tPress [Enter] to exit...\n");
getchar();
exit(EXIT_FAILURE);
}
for (j = 0; j < t->cols; j++){
data[i][j] = (char *)malloc(NAME_MAX_LEN * sizeof(char));
if(data[i][j] == NULL){
printf("\n\t\tERROR: Out of memory\n");
printf("\n\t\tPress [Enter] to exit...\n");
getchar();
exit(EXIT_FAILURE);
}
}
}
for(i = 0; i < t->rows; ++i){
for(j = 0; j < n; ++j)
data[i][j] = t->cell[i][j];
for(j = n; j < t->cols; ++i)
data[i][j] = t->cell[i][j+1];
free(t->cell[i][n]);
}
free(t->cell);
t->cell = data;
}
My doubt is should I add the line I added in red? it isn't there now and it seems to work perfectly but I don't know if it could be a memory leak not adding them and I should just free each of the strings for the cell that is being removed in each row?
Thanks guys!