Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

int **Matrix_B1;  

Matrix_B1 = (int**)malloc(RowB*(sizeof(int*)));  

for (int p=0; p<RowB; p++)  
{  
    Matrix_B1[p] = (int*)malloc(ColumnB*(sizeof(int)));   
}

How do I create a function to perform dynamic memory allocation in c ?

I have lots of matrices like this and I don't want to keep writing the same thing.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
250 views
Welcome To Ask or Share your Answers For Others

1 Answer

int *Matrix_B1;

int matrixSize = Rowb * ColumnB; Matrix_B1 = int(*)malloc(matrixSize*sizeof(int));

// writing values to matrix - Matrix_B1[4] = $Value;

// If there are lot of matrices, den you can store the pointers of every matrix in some array pf pointers. each matrix is implemented as 2D array and its memory is dynamically allocated .you can access data in row or column style.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...