Hi there. I'm new to the forum and this is my first time posting here. I'm a newbie at C and I'm experiencing a pointers problem. Here's the code:
It is supposed to be a program that receives a matrix and a vector and multiplies them. It compiles, but when it goes to the function Show_Matrix it shows completely different values, so the result is wrong. I've tried Splint debugger so I go the folling messages:Code:#include <stdio.h> float *mult_matrix(int n, float *p, float *q); float *get_matrix(int m, int n); void show_matrix(int m, int n, float *p); int main(void) { int n; float *a, *b, *x; printf("\nEnter the size of the linear system: \n"); scanf("%d", &n); a=get_matrix(n, n); show_matrix(n, n, a); b=get_matrix(n, 1); show_matrix(n, 1, b); x=mult_matrix(n, a, b); show_matrix(n, 1, x); return 0; } float *mult_matrix(int n, float *p, float *q) { int i, j; float X[n]; float *x; for(i=0; i<n; i++) { X[i]=0; } for (i=0; i<n; i++) { for(j=0; j<n; j++) { X[i]=X[i]+(*p)*(*q); p++; q++; } q=q-n; } x=X; return x; } float *get_matrix(int m, int n) { int i, j; float *a, A[m][n]; a=&A[0][0]; for (i=0; i<m; i++) { for (j=0; j<n; j++) { printf("\nEnter with the element a%d%d of the matrix:", i+1, j+1); scanf("%f", a); a++; } } a=a-m*n; return a; } void show_matrix(int m, int n, float *p) { int i, j; printf("\n"); for (i=0; i<m; i++) { printf("\n"); for (j=0; j<n; j++) { printf(" %f ", *p); p++; } } printf("\n"); }
Splint 3.1.2 --- 03 May 2009
matrix.c: (in function main)
matrix.c:13:2: Return value (type int) ignored: scanf("%d", &n)
Result returned by function call is not used. If this is intended, can cast
result to (void) to eliminate message. (Use -retvalint to inhibit warning)
matrix.c:22:11: Fresh storage a not released before return
A memory leak has been detected. Storage allocated locally is not released
before the last reference to it is lost. (Use -mustfreefresh to inhibit
warning)
matrix.c:15:2: Fresh storage a created
matrix.c:22:11: Fresh storage b not released before return
matrix.c:17:2: Fresh storage b created
matrix.c:22:11: Fresh storage x not released before return
matrix.c:19:2: Fresh storage x created
matrix.c: (in function mult_matrix)
matrix.c:37:9: Value X[] used before definition
An rvalue is used that may not be initialized to a value on some execution
path. (Use -usedef to inhibit warning)
matrix.c:45:9: Stack-allocated storage x reachable from return value: x
A stack reference is pointed to by an external reference when the function
returns. The stack-allocated storage is destroyed after the call, leaving a
dangling reference. (Use -stackref to inhibit warning)
matrix.c:43:2: Storage x becomes stack-allocated storage
matrix.c:45:9: Returned storage x not completely defined (*x is undefined): x
Storage derivable from a parameter, return value or global is not defined.
Use /*@out@*/ to denote passed or returned storage which need not be defined.
(Use -compdef to inhibit warning)
matrix.c:43:2: Storage *x allocated
matrix.c: (in function get_matrix)
matrix.c:57:4: Return value (type int) ignored: scanf("%f", a)
matrix.c:64:9: Returned storage a not completely defined (*a is undefined): a
matrix.c:52:2: Storage *a allocated
matrix.c:3:8: Function exported but not used outside matrix: mult_matrix
A declaration is exported, but not used outside this module. Declaration can
use static qualifier. (Use -exportlocal to inhibit warning)
matrix.c:46:1: Definition of mult_matrix
matrix.c:4:8: Function exported but not used outside matrix: get_matrix
matrix.c:65:1: Definition of get_matrix
matrix.c:5:6: Function exported but not used outside matrix: show_matrix
matrix.c:80:1: Definition of show_matrix
Finished checking --- 12 code warnings
Please, could somebody help me and tell me what's going on? Cheers.



LinkBack URL
About LinkBacks



