That is what I meant, which is conveniently also what the variable "data" in your function represents, so that's nice.
This is a discussion on Delete an element in a dynamic array? within the C Programming forums, part of the General Programming Boards category; That is what I meant, which is conveniently also what the variable "data" in your function represents, so that's nice....
That is what I meant, which is conveniently also what the variable "data" in your function represents, so that's nice.
Okeeey then now I understand it thanks a lot! I will try to get the delete_column to work now and I will come soon if I have problems, but I think the idea is pretty similar.. Thanks again!
Ok got the delete_column function coded now but I just have one doubt, here it is:
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?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; }
Thanks guys!
That is memory that you no longer want, so freeing is appropriate.
Hahah ok that's what I thought
Thanks then! everything is working as expected thank you very much for the help!