Yeah... of course, I have already done that but the only message I receive is segmentation fault, and I don't seem to find a null pointer anywhere... if I try to run a modified function which only sorts the first to rows it works the problems seems to come when I introduce the loop... The line it marks is the strcmp one, but I have no idea why... I'll post the "constructor" of the table and the struct as to see if it helps someone:
Code:
typedef struct{
int rows, cols;
unsigned long int id;
char *name;
char ***cell; // 3d array of chars or 2D array of strings
}table;
Code:
/* create_table: create a table with given rows and columns and return it */
table create_table(char *name, int rows, int cols){
int i, j;
static unsigned long int id;
table t;
clip(&rows, 1, TABLE_MAX_ROWS);
clip(&cols, 1, TABLE_MAX_COLS);
t.rows = rows;
t.cols = cols;
t.cell = (char ***) malloc(rows * sizeof(char **));
if(t.cell == NULL){
printf("\n\t\tERROR: Out of memory\n");
printf("\n\t\tPress any key to exit...\n");
getchar();
exit(EXIT_FAILURE);
}
for(i = 0; i < rows; ++i){
t.cell[i] = (char **) malloc(cols * sizeof(char *));
if(t.cell[i] == NULL){
printf("\n\t\tERROR: Out of memory\n");
printf("\n\t\tPress any key to exit...\n");
getchar();
exit(EXIT_FAILURE);
}
for (j = 0; j < cols; j++){
t.cell[i][j] = (char *)malloc(NAME_MAX_LEN * sizeof(char));
if(t.cell[i][j] == NULL){
printf("\n\t\tERROR: Out of memory\n");
printf("\n\t\tPress any key to exit...\n");
getchar();
exit(EXIT_FAILURE);
}
}
}
t.name = name;
t.id = id++;
return t;
}
Then the table is assigned when needed like:
Code:
table t;
t = create_table(...);
There's of course another function to delete the table when the program exists or the table is closed :P
P.S: I know it's not easy for me neither to show you were to start looking so all the info you need ask me please :P
Also I wanted to know first if the sort_table function looks right or if there's something wrong with it because I'm getting so confused