Well yes, I call initializematrix before insertnode. And as for the logic with the 4 if statements I understand how the overwrite that would cause which I am NOT trying to do and have fixed that. Lastly, I malloced ptr, but I did not think I needed too; I am still getting the same error in insertnode...
Here is my reformed code, that is still getting a bus error, I think with the for loops in insert node:
CODE
int main(int argc, char* argv[])
{
FILE* in;
long dimension, row_index, column_index;
in = fopen(argv[1], "r");
fscanf(in, "%ld", &dimension);
double value;
Matrix* M = malloc(sizeof(Matrix));
initializeMatrix(&M, &dimension,in);
while (fscanf(in, "%ld %ld %lf", &row_index, &column_index, &value) == 3) {
insertNode(&M, row_index, column_index, value);
}
long beginrow = 2, endrow=5, begincol=1, endcol=3;
printSubMatrix(M, beginrow, endrow, begincol, endcol);
return EXIT_SUCCESS;
}
CODE
void initializeMatrix(Matrix** M, long* dimension, FILE* in) {
(*M)->rowList = malloc(*dimension*sizeof(Node));
(*M)->columnList = malloc(*dimension*sizeof(Node));
int i;
for (i=0; i<*dimension; i++) {
(*M)->rowList[i] = NULL;
(*M)->columnList[i] = NULL;
}
}
void insertNode(Matrix** M, long row_index, long column_index, double value) {
Node* ptr = malloc(sizeof(Node));
ptr->value = value; /* initialize all properties of node */
ptr->rowIndex = row_index;
ptr->columnIndex = column_index;
if ((*M)->rowList[row_index] == NULL) { /* index is empty */
ptr->rowPtr = NULL;
(*M)->rowList[row_index] = ptr;
}
else { /* ((*M)->rowList[row_index] != NULL) { index is not empty */
ptr->rowPtr = NULL;
int counter = 0;
for (counter; (*M)->columnList[counter] != NULL; counter++) {}
((*M)->columnList[counter])->colPtr = ptr;
}
if ((*M)->columnList[column_index] == NULL) {
ptr->colPtr = NULL;
(*M)->columnList[column_index] = ptr;
}
else { /* ((*M)->columnList[column_index] != NULL) { */
ptr->colPtr = NULL;
int counter = 0;
for (counter; (*M)->rowList[counter] != NULL; counter++) {}
((*M)->rowList[counter])->rowPtr = ptr;
}
}
skaoth, thank you for all of your help so far; these bus errors have been giving me problems for days.
This post has been edited by strakerc: 20 Jul, 2008 - 03:16 PM