简体   繁体   中英

Can't pull my struct data to the main() function (C language)

I'm writing Matrix library, I have MatrixMultiplication(Mat a, Mat b) function that returns Mat

inside the function everything is working correctly but when I'm pulling it to the main all the calculation disappears, although I'm using pointers.

my Mat struct is using pointer to a 1d array and n x k values for sizing.


INPUT:


#include <stdio.h>
#include <stdlib.h>
#include <time.h> 
#define size 100
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////       ALL THIS FUNCTIONS WORK PERFECTLY       //////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int RandomRange(int l,int u){int a = (rand()%(u-l+1))+l;return a;}
void ArrayRandomizer(int *a ,int s,int l,int u){for(int i = 0; i < s; i++){
    int random = RandomRange(l,u);int *ptr2 = &random;Swap(a+i,ptr2);}}
typedef struct Mat {int * arr, rows, cols;} Mat;
Mat MatrixBuilder (int *a,int r,int c){Mat new;new.arr = a;new.rows = r;new.cols = c;return new;}
void PrintMatrix (Mat a){
    printf("\nMatrix %dx%d :\n",a.rows,a.cols);
    for (int i = 1, j = 0; i <= a.rows; i++ ,j = j + a.cols ){
    for (; j < (a.cols*a.rows); j++){
    if ((j+i)%a.cols == 1){printf(" [");}
    printf("%d ,",*(a.arr+(j+i-1)));
    if ((j+i)%a.cols == 0){printf("]\n");}}}}
int MatrixValueOfCell (Mat a,int r,int c){
    if (r > a.rows-1){printf("\n*****MatrixValueOfCell ROW IS OUT OF THE MATRIX , USE SMALLER INTEGER");return -9999999;}
    if (c > a.cols-1){printf("\n*****MatrixValueOfCell COL IS OUT OF THE MATRIX , USE SMALLER INTEGER");return -9999999;}
    if (r < 0){printf("\n*****MatrixValueOfCell CAN'T USE NEGATIVE ROW INDEX , USE BIGGER INTEGER");return -9999999;}
    if (c < 0){printf("\n*****MatrixValueOfCell CAN'T USE NEGATIVE COL INDEX , USE BIGGER INTEGER");return -9999999;}
    return *(a.arr+((r)*a.cols)+(c));}
void setMatrixValueOfCell (int s,Mat a,int r,int c){
    if (r > a.rows-1){printf("\n*****MatrixValueOfCell ROW IS OUT OF THE MATRIX , USE SMALLER INTEGER");}
    else if (c > a.cols-1){printf("\n*****MatrixValueOfCell COL IS OUT OF THE MATRIX , USE SMALLER INTEGER");}
    else if (r < 0){printf("\n*****MatrixValueOfCell CAN'T USE NEGATIVE ROW INDEX , USE BIGGER INTEGER");}
    else if (c < 0){printf("\n*****MatrixValueOfCell CAN'T USE NEGATIVE COL INDEX , USE BIGGER INTEGER");}
    else {*(a.arr+((r)*a.cols)+(c)) = s ;}}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Mat MatrixMultiplication (Mat a,Mat b){
int q [a.rows*b.cols]; int * ptr = q;
ArrayRandomizer(ptr,a.rows*b.cols,0,0);
Mat c = MatrixBuilder (ptr,a.rows,b.cols);
for (int i = 0; i < a.rows; i++){
    for (int j = 0; j < b.cols; j++){
        for (int k = 0; k < a.cols; k++){
            setMatrixValueOfCell((MatrixValueOfCell(c,i,j)+(MatrixValueOfCell(a,i,k)*MatrixValueOfCell(b,k,j))),c,i,j);}}}

printf("\n________________________________\nDirect From Function :");
PrintMatrix(c);
printf("________________________________\n");
return c;

}



int main (void){

int a[size];ArrayRandomizer(a,size,0,46);int * ptr = a;
Mat test = MatrixBuilder(ptr , 4 , 3);

int b[size];ArrayRandomizer(b,size,0,9);int * ptr2 = b;
Mat test2 = MatrixBuilder(ptr2 , 3 , 4);

PrintMatrix(test);
PrintMatrix(test2);

Mat Result = MatrixMultiplication(test,test2);

PrintMatrix(Result);

}




OUTPUT:


Matrix 4x3 :
    [38 ,4 ,18 ,]
    [23 ,17 ,26 ,]
    [16 ,3 ,28 ,]
    [28 ,10 ,17 ,]

Matrix 3x4 :
    [5 ,0 ,4 ,8 ,]
    [7 ,1 ,7 ,2 ,]
    [7 ,2 ,2 ,6 ,]

________________________________
Direct From Function :
Matrix 4x4 :
    [344 ,40 ,216 ,420 ,]
    [416 ,69 ,263 ,374 ,]
    [297 ,59 ,141 ,302 ,]
    [329 ,44 ,216 ,346 ,]
________________________________

Matrix 4x4 :
    [1 ,0 ,1 ,0 ,]
    [2 ,0 ,3 ,0 ,]
    [297 ,59 ,10 ,0 ,]
    [1564489376 ,32593 ,1979723792 ,21998 ,]



I also tried returning a pointer instead of Mat struct, and then made a new Matrix using this pointer and it didn't work, I would like to know how this could be fixed without allocating dynamic memory with functions like malloc() calloc() thanks:)

by the way I'm using Ubuntu on virtual machine if this connected somehow

In MatrixMultiplication function, the array q is allocated on the stack. So at function exit, its memory is freed and matrix datas may be replaced with other values. The solution is to allocate memory with malloc so it is kept even outside of the function.

From your code, I just removed the variable q and replace ptr with a memory allocation:

Mat MatrixMultiplication (Mat a,Mat b){
int * ptr = malloc(a.rows * b.cols * sizeof(*ptr));
ArrayRandomizer(ptr,a.rows*b.cols,0,0);
Mat c = MatrixBuilder (ptr,a.rows,b.cols);
for (int i = 0; i < a.rows; i++){
    for (int j = 0; j < b.cols; j++){
        for (int k = 0; k < a.cols; k++){
            setMatrixValueOfCell((MatrixValueOfCell(c,i,j)+(MatrixValueOfCell(a,i,k)*MatrixValueOfCell(b,k,j))),c,i,j);}}}

printf("\n________________________________\nDirect From Function :");
PrintMatrix(c);
printf("________________________________\n");
return c;

}

Beware also that the this code just to point out the problem you exposed. It can't be a global solution because it leads to another problem: memory leak.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM