<filler>Originally Posted by nadroj
<filler>Originally Posted by nadroj
Header File:
Function Decelerations:Code:/* File: matrix.h */ #ifndef MATRIXh #define MATRIXh #include <stdio.h> #include <stdlib.h> #include <assert.h> #define FORMAT "%8.3lf" typedef struct { int rowdim, coldim; double** element; } matrix; /* function prototypes */ matrix create_empty(int rdim, int cdim); matrix create_initval(int rdim, int cdim, double* values); void destroy(matrix); void matrix_print(matrix a); double retrieve(int row, int col, matrix m); /* retrieve an element from m */ /* void assign(int row, int col, matrix*, T val); /* assign a value to an element of m */ void equate(matrix* m1, matrix* m2); /* m1 = m2 */ matrix add(matrix, matrix); matrix subtract(matrix, matrix); matrix negate(matrix); matrix multiply(matrix, matrix); /* matrix scalar_multiply(T scalar, matrix); */ /* remaining function prototypes not shown */ #endif
Test File:Code:#include "matrix.h" matrix create_empty(int rdim, int cdim) { matrix new_matrix; new_matrix.element = malloc(sizeof(matrix)); new_matrix.rowdim = rdim; new_matrix.coldim = cdim; new_matrix.element = malloc((rdim*cdim) * sizeof(double)); return new_matrix; } matrix create_initval(int rdim, int cdim, double* values) { int i, j, k; matrix new_matrix; new_matrix.rowdim = rdim; new_matrix.coldim = cdim; new_matrix.element = malloc(sizeof(double*) * rdim); for (k=0; k<rdim; k++) new_matrix.element[k] = malloc(sizeof(double) * cdim); for (i=0; i<cdim; i++) { for(j=0; j<rdim; j++) new_matrix.element[i][j] = values[i][j]; // does not work { return new_matrix; } void destroy(matrix a) { free(a.element); } void matrix_print(matrix a) { int i, j; printf("\n"); for (i=0; i<a.coldim; i++) { for(j=0; i<a.rowdim; j++) { printf(" %f ", a.element[i][j]); } } } } }
Errors:Code:#include "matrix.h" int main() { double one[2][3] = { {1,2,3}, {4,5,6} }; matrix m_one; m_one = create_initval(2, 3, one); matrix_print(m_one); return 0; }
matrix.c: In function ‘create_initval’:
matrix.c:25: error: subscripted value is neither array nor pointer
test.c: In function ‘main’:
test.c:12: warning: passing argument 3 of ‘create_initval’ from incompatible pointer type
Originally Posted by nadrojOriginally Posted by M-S-HThis is why I say to post all of your code. Because I told you how to fix it, then you said you fixed it and say the error is still there. Its quite frustrating when I make suggestions and you ignore them.Originally Posted by M-S-H
EDIT: Also, you still need to "free" the values properly, or there will be memory leaks (I described this earlier). Also, you should verify that "malloc" doesn't return "null) (as described in the link I gave earlier).
Last edited by nadroj; 02-15-2010 at 10:49 PM.
I changed all my code to
Code:/* File: matrix.h */ #ifndef MATRIXh #define MATRIXh #include <stdio.h> #include <stdlib.h> #include <assert.h> #define FORMAT "%8.3lf" typedef struct { int rowdim, coldim; double** element; } matrix; /* function prototypes */ matrix create_empty(int rdim, int cdim); matrix create_initval(int rdim, int cdim, double values[rdim][cdim]); void destroy(matrix); void matrix_print(matrix a); double retrieve(int row, int col, matrix m); /* retrieve an element from m */ /* void assign(int row, int col, matrix*, T val); /* assign a value to an element of m */ void equate(matrix* m1, matrix* m2); /* m1 = m2 */ matrix add(matrix, matrix); matrix subtract(matrix, matrix); matrix negate(matrix); matrix multiply(matrix, matrix); /* matrix scalar_multiply(T scalar, matrix); */ /* remaining function prototypes not shown */ #endifCode:#include "matrix.h" matrix create_empty(int rdim, int cdim) { matrix new_matrix; new_matrix.element = malloc(sizeof(matrix)); new_matrix.rowdim = rdim; new_matrix.coldim = cdim; new_matrix.element = malloc((rdim*cdim) * sizeof(double)); if(new_matrix.element == NULL) { fprintf(stderr, "out of memory\n"); exit(0); } return new_matrix; } matrix create_initval(int rdim, int cdim, double values[rdim][cdim]) { int i, j, k; matrix new_matrix; new_matrix.rowdim = rdim; new_matrix.coldim = cdim; new_matrix.element = malloc(sizeof(double*) * rdim); if(new_matrix.element == NULL) { fprintf(stderr, "out of memory\n"); exit(0); } for (k=0; k<rdim; k++) new_matrix.element[k] = malloc(sizeof(double) * cdim); if(new_matrix.element[k] == NULL) { fprintf(stderr, "out of memory\n"); exit(0); } for (i=0; i<cdim; i++) { for(j=0; j<rdim; j++) new_matrix.element[i][j] = values[i][j]; // does not work { return new_matrix; } void destroy(matrix a) { free(a.element); } void matrix_print(matrix a) { int i, j; printf("\n"); for (i=0; i<a.coldim; i++) { for(j=0; i<a.rowdim; j++) { printf(" %f ", a.element[i][j]); } } } } }now the array seems to be passed in fine, but theres a problem with the print statement. when i compile both .c files, i get this:Code:#include "matrix.h" int main() { double one[2][3] = { {1,2,3}, {4,5,6} }; matrix m_one; m_one = create_initval(2, 3, one); matrix_print(m_one); return 0; }
/tmp/cce6gQuE.o: In function `main':
test.c.text+0x7d): undefined reference to `matrix_print'
collect2: ld returned 1 exit status
In "create_initval"The brace there should probably be a close one, not an open one. After fixing that, if you get other errors, make sure all of your brackets line up.Code:for (i=0; i<cdim; i++) { for(j=0; j<rdim; j++) new_matrix.element[i][j] = values[i][j]; // does not work {
i fixed the bracket, and it compiled fine, but when i ran it,
a null must have been returned because it displayed "out of memory"
now how do i fix that?
I told you before that you should make the array argument to "create_initval" to "double values[2][3]", if you know that the values are going to be 2, 3. In this case, you don't need to pass the dimensions, since they are fixed at 2, 3.
It is unlikely that they will probably be fixed dimensions. In this case, you need to make the array "one" in "main" a dynamic 2-d array, also, just like the "element" 2-d "array". Sowould becomeCode:double one[2][3];then you allocate it in the same way as the "element" array. First the rows, then the columns. Doing it this way requires you to keep the dimensions as arguments to "create_initval", but the trade off is that the array can be arbitrary non-constant size. So the last argument inCode:double ** one;would become "double ** values", and you should be able to use it the same way you are now. "one" would also be passed the same way it is now, once you change the rest to use **.Code:matrix create_initval(int rdim, int cdim, double values[rdim][cdim])
I'm leaving now, so I'll let you do some independent thinking/learning and debugging.
Last edited by nadroj; 02-15-2010 at 11:41 PM.
Alright, thanks so much for all your help. I really appreciate it.