malloc a array in C with loop dynanic array(2D) -
it compiles no errors when try execute when reaches part of code freaks out , stops.(crashes , exe doesn't respond)
//creation of array 2 lines_2 x cols_2 size array_2 = (int **)malloc(lines_2 * sizeof(int*)); for( i=0; i< lines_2;i++) { array_2[i] = (int*) malloc(cols_2 * sizeof(int)); }
full code trying create program multiplies 2 dynamic arrays.
#include <stdio.h> #include <stdlib.h> int checkifmul(int ,int );//intx2 void fill( int **,int ,int );//ar intx2 void arraymuarray( int **, int **, int **,int,int ); int main(){ int i,lines_1,lines_2,cols_1,cols_2,**array_1,**array_2,**array_r1,**array_r2; //input do{ do{ printf("enter lines of first array.\n"); scanf(" %d/n",&lines_1); }while(lines_1 <1); do{ printf("enter columns of first array.\n"); scanf(" %d/n/n",&cols_1); }while(cols_1 >5); do{ printf("enter lines of second array.\n"); scanf(" %d/n",&lines_2); }while(lines_1 <1); do{ printf("enter columns of second array.\n"); scanf(" %d/n/n",&cols_2); }while(cols_2 >5); }while(checkifmul(lines_1,cols_2)==0); printf("here"); //creation of array 1 lines_1 x cols_1 size array_1 = (int **)malloc(lines_1 * sizeof(int*)); for( i=0; i<lines_1;i++){ array_2[i] = (int *)malloc(cols_1 * sizeof(int)); } //creation of array 2 lines_2 x cols_2 size array_2 = (int **)malloc(lines_2 * sizeof(int*)); for( i=0; i< lines_2;i++){ array_2[i] = (int*) malloc(cols_2 * sizeof(int)); } //creating array mult array_1 * array_2 = array_r1 array_r1 = (int **)malloc(lines_1 * sizeof(int*)); for( i=0; i< lines_1;i++){ array_r1[i] = (int*) malloc(cols_2 * sizeof(int)); } //creating array mult array_r1 * = array_r2 array_r2 = (int **)malloc(lines_1 * sizeof(int*)); for( i=0; i< lines_1;i++){ array_r2[i] = (int*) malloc(cols_2 * sizeof(int)); } fill(array_1,lines_1,cols_1);//user fills array 1 fill(array_2,lines_2,cols_2);//user fills array 2 arraymuarray(array_1,array_2,array_r1,lines_1,cols_2);//multiplier return 0; } int checkifmul (int lines,int cols)//checks if arrays 1 , 2 can multiplied { if(lines==cols){ //true return 1; }else{ //flase printf("\athese arrays can't multiplied.\n\n"); return 0; } } void fill(int **array,int lines,int cols){//asks user input int dynamic array int i,j; for( i=0;i<lines;i++){ for( j=0;j<cols;j++){ printf("enter value position (%d,%d).",i,j); scanf(" %d/t",&array[i][j]); } printf("\n"); } } void arraymuarray(int **array_1,int **array_2,int **array_r1,int lines,int cols){ int i,j,u,s; for( i=0;i<lines;i++){ for( j=0;j<cols;j++){ for( u=0;u<cols;u++){ s = s + (array_1[i][u] * array_2[u][j]); } array_r1[i][j] = s; s = 0; } } }
in part of code problem...
array_1 = (int **)malloc(lines_1 * sizeof(int*)); for( i=0; i<lines_1;i++){ array_2[i] = (int *)malloc(cols_1 * sizeof(int)); }
since line.
array_2[i] = (int *)malloc(cols_1 * sizeof(int));
should be.
array_1[i] = (int *)malloc(cols_1 * sizeof(int));
because @ point haven't allocated memory array_2 yet.
note: should read this, it's not recommended cast malloc.
Comments
Post a Comment