I've been trying this code and it's not working out very well.
void *matrix_allocate_variable (int size)
{
void *p1;
if ((p1=(int*)malloc(size))==NULL){
printf("out of memory.\n");
exit(1);
}
return p1;
}
Here I created a function that call's malloc and exits upon error, so that I could use it in my next function:
void matrix_new(int **matrices, int *row_counts, int *column_counts, char specifier)
{
int index, i;
index= (int)(specifier-'A');
scanf("%d",&row_counts[index]);
scanf("%d",&column_counts[index]);
matrices[index]= (int*)matrix__allocate_variable(sizeof(int)* (row_counts[index]*column_counts[index]);
Here is where I am having problems. I'm trying to have the user enter some input for creating the matrix, but I'm having lots of problems trying to get this working. Can some one help me start this off?
PS. For more details, I'm creating functions in functions.c, this are what I have so far. I have a main.c which calls these functions so that later on I can add, subract, and transpose, but as of now I am trying to enter in data which is what I'm having lot of trouble with. Here is my main.c where I call the functions.
/* Pointer to the set of matrix registers. */
int **matrices = NULL;
/* row_counts[i] stores the number of rows in matrix i */
int *row_counts = NULL;
/* column_counts[i] stores the number of columns in matrix i */
int *column_counts = NULL;
/**********************************************************************
Skeleton code part B: suggested form for selected variable initializations
**********************************************************************/
/* Initialize the matrix set. */
matrices = (int**) matrix_allocate_variable(...);
column_counts = (int *)matrix_allocate_variable(...);
row_counts = (int *)matrix_allocate_variable(...);
char call[2];
int error = 2;
do {
printf ( "> ");
if (scanf ("%1s", call) !=1) {
fprintf (stderr, "Command not found. \n");
exit (1);
}
switch (call [0]) {
case 'A': matrix_new(matrices,row_counts,column_counts,'A');
break;
case 'B': matrix_new(matrices,row_counts,column_counts,'B');
break;
case 'C': matrix_new(matrices,row_counts,column_counts,'C');
break;
case 'D': matrix_new(matrices,row_counts,column_counts,'D');
break;
case '+': matrix_add(matrices,row_counts,column_counts);
break;
case '^': matrix_tranpose(matrices,row_counts,column_counts);
break;
case '*': matrix_multiply(matrices,row_counts,column_counts);
break;
case '$': exit (1);
default : fprintf (stderr, "Command not found. \n");
}
} while (error != 1);
return 0;
}
Any help will be good and any pointers in what I should do next is great also. Thank you so so much every one.
Hi this is a sample code to create one matrix using malloc.
(This should give you some insight on how to create an array of matrices. If it doesn't then let me know.)
#include <stdio.h>
#include <stdlib.h>
// Creates a matrix given the size of the matrix (rows * cols)
int **CreateMatrix(int rows, int cols) {
int **matrix = malloc(sizeof(int*) * rows);
int row;
for (row = 0; row < rows; row++) {
matrix[row] = malloc(sizeof(int) * cols);
}
return matrix;
}
// Take input for the matrix.
void MatrixInput(int **matrix, int rows, int cols) {
int row, col;
for (row = 0; row < rows; row++) {
for (col = 0; col < cols; col++) {
scanf("%d", &matrix[row][col]);
}
}
}
void PrintMatrix(int **matrix, int rows, int cols) {
int row, col;
for (row = 0; row< rows; row++) {
for (col = 0; col < cols; col++) {
printf("%d ", matrix[row][col]);
}
printf("\n");
}
}
int main() {
int **matrix;
int rows = 5;
int cols = 4;
matrix = CreateMatrix(rows, cols);
MatrixInput(matrix, rows, cols);
PrintMatrix(matrix, rows, cols);
}
Here is an example of creating and freeing memory for a two dimendional array...
(easily modifiable for other types)
int ** Create2D(int **arr, int cols, int rows)
{
int space = cols*rows;
int y;
arr = calloc(space, sizeof(int));
for(y=0;y<cols;y++)
{
arr[y] = calloc(rows, sizeof(int));
}
return arr;
}
void free2DInt(int **arr, int cols)
{
int i;
for(i=0;i<cols; i++)
if(arr[i]) free(arr[i]);
free(arr);
}
Use it like this:
#include <ansi_c.h>
int main(void)
{
int **array=0, i, j;
array = Create2D(array, 5, 4);//get the actual row/column values from reading the file once.
for(i=0;i<5;i++)
for(j=0;j<4;j++)
array[i][j]=i*j; //example values for illustration
free2DInt(array, 5);
return 0;
}
Related
i defined a matrix into a function. how do i return that matrix for print it when i call it with another function. i mean...
#include<stdio.h>
#include<conio.h>
#include<time.h>
void main() {
int m,n;
printf("type 2 numbers:");
scanf("%i %i",&m,&n);
declaration(m,n);\\HERE IS THE PROBLEM
printing(matrix,m,n);
getch();
}
void declaration(int a,int b) {
srand(time(NULL));
int i,j,matrix[a][b];
for(i=0;i<a;i++){
for(j=0;j<b;j++){
matrix[i][j]=1+rand()%7;
}
}
}
void printing(int c[100][100],int a,int b) {
int i,j;
for(i=0;i<a;i++){
for(j=0;j<b;j++){
printf("%i\t",c[i][j]);
}
printf("\n");
}
}
Define it like:
typedef struct {
int rows;
int cols;
int *data;
} int_matrix_entity, *int_matrix;
int_matrix int_matrix_create(int rows, int cols, bool rand)
{
int_matrix mt;
int i;
if ((mt = malloc(sizeof(int_matrix_entity))) == NULL)
{
return NULL;
}
if ((mt->data = malloc(sizeof(int) * cols * rows)) == NULL)
{
free(mt);
return NULL;
}
if (rand)
{
srand(time(NULL));
for (i = 0; i < cols * rows; i++)
{
mt->data[i] = 1 + rand() % 7;
}
}
else
{
memset(mt->data, 0, sizeof(int) * cols * rows);
}
return mt;
}
void int_matrix_printf(int_matrix mt)
{
int i;
int j;
for (i = 0; i < mt->rows; i++)
{
for (j = 0; j < mt->cols; j++)
{
printf("%5d ", mt[i * cols + j]);
}
printf("\n");
}
}
You have a few points that require a bit more attention;
1 ) read warning and error messages given by your compiler
2 ) again, read warning messages given by your compiler
3 ) use indentation to make your code more readable.
4 ) Always return from main(), that's a good practice
The code below does what you want to achieve; have a look at it and keep on reading...
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
// You either have to declare your functions
// or implement them before main()
void declaration(int a,int b, int m[a][b]);
void printing(int a,int b, int m[a][b]);
int main(){ // always return from main()
int m,n;
printf("type 2 numbers:");
scanf("%i %i",&m,&n);
int matrix[m][n];
declaration(m, n, matrix);
printing(m, n, matrix);
return 0;
}
void declaration(int a,int b, int m[a][b]){
srand(time(NULL));
int i,j;
for(i=0;i<a;i++){
for(j=0;j<b;j++){
m[i][j]=1+rand()%7;
}
}
}
void printing(int a,int b, int m[a][b]){
int i,j;
for(i=0;i<a;i++){
for(j=0;j<b;j++){
printf("%i\t",m[i][j]);
}
printf("\n");
}
}
You need a way to transfer data from one function to another. You cannot simply declare an auto variable in one function and pass it to another as you did in the code below
declaration(m,n);
printing(matrix,m,n); /* where does matrix[][] come from? */
remember, C is a strongly typed language which means you have to declare your variables before using them. This applies to your functions as well. You either have to give your function declarations before main() (or more specifically, before using them), or implement them.
Look into your header files (i.e. .h files) and you will see lots of function declarations.
Since you use variable length arrays, make sure your compiler is at least capable of compiling code confirming C99 standard.
Some extras;
Normally, C passes arguments by value and you have to use a pointer if you want the value of your variable get changed within the function. If you have a close look at the code snippet I gave, I simply used an int m[a][b].In C, the name of an array is a pointer to its first element, hence you can change the value of array elements when actually array's name is passed to your function as an argument.
For further reading, you may want to look at
variable scope
global variables (you can define matrix[][] as a global variable and change the value of matrix elements)
declaration vs definition in C
Another simple way to do it is use double pointer to create 2-dimensional array. Keep it simple.
#include <stdio.h>
#include <stdlib.h>
int** create_matrix(int rows, int cols) {
int **matrix = malloc(rows*(sizeof(int *)));
for(int i = 0; i < rows; i++) {
matrix[i] = malloc(cols*sizeof(int));
}
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
matrix[i][j] = 1 + rand()%7;
}
}
return matrix;
}
void printing(int** matrix, int rows, int cols) {
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int main(void) {
int rows, cols;
rows = 3, cols = 3;
int** matrix = create_matrix(rows, cols);
printing(matrix, rows, cols);
free(matrix);
return 0;
}
I'm having a big troubles figuring out how to rightfully create a 2D dynamic array, how to assert the memory and how to free it in the end.
I'll show you parts of my code, and please tell me what I'm doing wrong.
I declare on the dynamic array in main function and send it to BuildMatrix function that is supposed to assert the needed memory to the array and fill it.
that's how I declared on the array and send it to the function Build:
int row, column, i, j;
int **matrix;
BuildMatrix(&matrix, row, column);
now thats BuildMatrix decleration:
void BuildMatrix(int*** matrix, int row, int column);
And that's how I assert the memory (row and column have values that the user chose)
matrix =malloc(row * sizeof(int *));
for (i = 0; i < row; i++)
matrix[i] =malloc(column * sizeof(int));
Now so far everything works just fine, but when I try to free the memory, I get break point error
That's the function I used for freeing the memory:
void ExitAndFree(int** matrix, int row) {
int i;
for (i = 0; i < row; i++) {
free(matrix[i]);
}
free(matrix);
}
The debugger show me that the error is on the first free (if I remove the first one, the second gives the error)
There is also another problem, but I think its to much for now, I'll ask later... :P
Thanks for your help!!
P.S: If you got any good tutorials about pointers and dynamic arrays (I'd rather 2D+ arrays) I'd appreciate it a lot.
I do not think you need a *** pointer here, instead BuildMatrix could just return ** to main(). This design change would make your program easier, as working with *** can be a pain sometimes.
You also are not checking the return values of malloc() and scanf(), which can lead to future problems, and it's just safer to check those first. I also suggest you Don't cast result of malloc(), as this is not really needed in C.
With your errors of free() from the code you posted on #flintlock's answer, There is an inconsistency in your code:
You have declared:
void ExitAndFree(int** matrix, int row)
When this should be instead:
void ExitAndFree(int*** matrix, int row)
This change is needed in your code because you are calling &matrix in main for ExitAndFree(), so having **matrix in this function is not good enough. Again, this is because the code is using ***, which makes life harder.
Your code seems to work here with this change.
With these recommendations, you can also implement your program like this:
#include <stdio.h>
#include <stdlib.h>
int **BuildMatrix(int row, int column);
void PrintAndFree(int **matrix, int row, int column);
int main(void) {
int **matrix, row, column;
printf("\nPlease enter number of rows:\n");
if (scanf("%d", &row) != 1) {
printf("Invalid rows.\n");
exit(EXIT_FAILURE);
}
printf("\nPlease enter number of columns:\n");
if (scanf("%d", &column) != 1) {
printf("Invalid columns.\n");
exit(EXIT_FAILURE);
}
matrix = BuildMatrix(row, column);
PrintAndFree(matrix, row, column);
return 0;
}
int **BuildMatrix(int row, int column) {
int **matrix, rows, cols;
matrix = malloc(row * sizeof(*matrix));
if (matrix == NULL) {
printf("Cannot allocate %d rows for matrix.\n", row);
exit(EXIT_FAILURE);
}
for (rows = 0; rows < row; rows++) {
matrix[rows] = malloc(column * sizeof(*(matrix[rows])));
if (matrix[rows] == NULL) {
printf("Cannot allocate %d columns for row.\n", column);
exit(EXIT_FAILURE);
}
}
printf("\nPlease enter values to the matrix:\n");
for (rows = 0; rows < row; rows++) {
for (cols = 0; cols < column; cols++) {
if (scanf("%d", &matrix[rows][cols]) != 1) {
printf("Invalid value entered.\n");
exit(EXIT_FAILURE);
}
}
}
return matrix;
}
void PrintAndFree(int **matrix, int row, int column) {
int rows, cols;
printf("\nYour matrix:\n");
for (rows = 0; rows < row; rows++) {
for (cols = 0; cols < column; cols++) {
printf("%d ", matrix[rows][cols]);
}
free(matrix[rows]);
matrix[rows] = NULL;
printf("\n");
}
free(matrix);
matrix = NULL;
}
Here a copy&paste ready example...
#include <stdlib.h>
void BuildMatrix(int*** matrix, int row, int column) {
*matrix = malloc(row * sizeof(int *));
int i;
for (i = 0; i < row; i++)
(*matrix)[i] = malloc(column * sizeof(int));
}
void ExitAndFree(int** matrix, int row) {
int i;
for (i = 0; i < row; i++) {
free(matrix[i]);
}
free(matrix);
}
int main()
{
int row = 10, column = 10;
int **matrix;
BuildMatrix(&matrix, row, column);
ExitAndFree(matrix, row);
return 0;
}
However I would strongly recommend against working with raw pointers and separate variables for matrix directly. A better solution Software Engineering wise would be to either use an existing matrix library, or writer your own matrix library in C with struct...
My question is regrading the second function
float *rowavg(float *matrix,int rows,int cols)
So i'm suppose to dynamically allocate an array of floats with row elements and return NULL if allocation fails. I think i did this right, right? The other part that i'm trying to do is set the ith element of the new array to the average of the values in the ith row of the previous array. This part is where I'm getting got up. Did i call the previous array correctly(I believe no)?
#include <stdio.h>
#include <stdlib.h>
float *readMatrix(int rows, int cols);
float *rowavg(float *matrix, int rows, int cols);
float *colavg(float *matrix, int rows, int cols);
#define MAX_DIM 10
int main(void)
{
int done = 0;
int rows, cols;
float *dataMatrix;
float *rowAveVector;
float *colAveVector;
float overallAve;
while (!done)
{
do
{
printf("Enter row dimension (must be between 1 and %d): ", MAX_DIM);
scanf("%d", &rows);
} while(rows <= 0 || rows > MAX_DIM);
do
{
printf("Enter column dimension (must be between 1 and %d): ", MAX_DIM);
scanf("%d", &cols);
} while(cols <= 0 || cols > MAX_DIM);
dataMatrix = readMatrix(rows, cols);
if (dataMatrix == NULL)
{
printf ("Program terminated due to dynamic memory allocation failure\n");
return (0);
}
rowAveVector = rowAverage(dataMatrix, rows, cols);
colAveVector = colAverage(dataMatrix, rows, cols);
if(rowAveVector == NULL || colAveVector == NULL)
{
printf("malloc failed. Terminating program\n");
return (0);
}
}
float *readMatrix(int rows, int cols)
//Dynamically allocate an array to hold a matrix of floats.
//The dimension of the matrix is numRows x numCols.
//If the malloc fails, return NULL.
//Otherwise, prompt the user to enter numRows*numCols values
//for the matrix in by-row order.
//Store the values entered by the user into the array and
//return a pointer to the array.
{
int i=0;
int j=0;
int elements=0;
float *m=malloc(rows*cols*sizeof(float));
if (m==NULL)
{
printf("error\n");
return NULL;
}
printf("Enter values for the matrix: ");
for (i=0;i<rows;i++)
{
for (j=0;j<cols;j++)
{
elements = i*cols+j;
scanf("%f", &m[elements]);
}
}
return m;
}
float *rowavg(float *matrix, int rows, int cols)
{
int i=0;
float mean=0;
float *mat=malloc(rows*sizeof(float));
if(mat==NULL)
{
printf("error\n");
return NULL;
}
for (i=0;i<rows;i++)
{
readMatrix(rows,cols);
mean=
mat[i]=mean;
}
}
First of all, call of readMatrix(rows,cols); not needed in rowavg.
And if you want rowavg to return average values for each row in your matrix, try the following:
float *rowavg(float *matrix, int rows, int cols)
{
// check matrix befor use
if (matrix == NULL)
{
return NULL;
}
int i=0;
int j=0;
double mean; // variable name from original code in the question
float *avgarr = (float *)malloc(rows*sizeof(float));
if(avgarr == NULL)
{
return NULL;
}
for (i=0;i<rows;i++)
{
mean = 0.0;
for (j=0;j<cols;j++)
{
mean += matrix[i*cols+j];
}
avgarr[i] = (float)(mean/cols);
}
return avgarr;
}
And because in main you already have
rowAveVector = rowAverage(dataMatrix, rows, cols);
if(rowAveVector == NULL)
{
printf("malloc failed. Terminating program\n");
return (0); // here consider return value different from 0
}
you should not use printf("error\n"); in rowavg.
And think over using free after allocatied memory not needed any more.
I have a pointer to a pointer ("paths") and I want to reallocate each pointer (each "path"). But I get a crash. Generally I am trying to find all possible powers of a number, which one can compute for some amount of operations (e.g for two operations we can get power of three and four (one operation for square of a number, then another one either for power of three or four)). I figured out how to do it on paper, now I am trying to implement it in code. Here is my try:
#include <stdio.h>
#include <stdlib.h>
void print_path(const int *path, int path_length);
int main(void)
{
fputs("Enter number of operations? ", stdout);
int operations;
scanf("%i", &operations);
int **paths, *path, npaths, npath;
npaths = npath = 2;
path = (int*)malloc(npath * sizeof(int));
paths = (int**)malloc(npaths * sizeof(path));
int i;
for (i = 0; i < npaths; ++i) // paths initialization
{
int j;
for (j = 0; j < npath; ++j)
paths[i][j] = j+1;
}
for (i = 0; i < npaths; ++i) // prints the paths, all of them are displayed correctly
print_path(paths[i], npath);
for (i = 1; i < operations; ++i)
{
int j;
for (j = 0; j < npaths; ++j) // here I am trying to do it
{
puts("trying to reallocate");
int *ptemp = (int*)realloc(paths[j], (npath + 1) * sizeof(int));
puts("reallocated"); // tried to write paths[j] = (int*)realloc...
paths[j] = ptemp; // then tried to make it with temp pointer
}
puts("memory reallocated");
++npath;
npaths *= npath; // not sure about the end of the loop
paths = (int**)realloc(paths, npaths * sizeof(path));
for (j = 0; j < npaths; ++j)
paths[j][npath-1] = paths[j][npath-2] + paths[j][j];
for (j = 0; j < npaths; ++j)
print_path(paths[j], npath);
puts("\n");
}
int c;
puts("Enter e to continue");
while ((c = getchar()) != 'e');
return 0;
}
void print_path(const int *p, int pl)
{
int i;
for (i = 0; i < pl; ++i)
printf(" A^%i -> ", p[i]);
puts(" over");
}
I am not sure the problem resides with the call to realloc(), rather you are attempting to write to locations for which you have not created space...
Although you create memory for the pointers, no space is created (allocate memory) for the actual storage locations.
Here is an example of a function to allocate memory for a 2D array of int:
int ** Create2D(int **arr, int cols, int rows)
{
int space = cols*rows;
int y;
arr = calloc(space, sizeof(int));
for(y=0;y<cols;y++)
{
arr[y] = calloc(rows, sizeof(int));
}
return arr;
}
void free2DInt(int **arr, int cols)
{
int i;
for(i=0;i<cols; i++)
if(arr[i]) free(arr[i]);
free(arr);
}
Use example:
#include <ansi_c.h>
int main(void)
{
int **array=0, i, j;
array = Create2D(array, 5, 4);
for(i=0;i<5;i++)
for(j=0;j<4;j++)
array[i][j]=i*j; //example values for illustration
free2DInt(array, 5);
return 0;
}
Another point here is that it is rarely a good idea to cast the return of [m][c][re]alloc() functions
EDIT
This illustration shows my run of your code, just as you have presented it:
At the time of error, i==0 & j==0. The pointer at location paths[0][0] is uninitialized.
EDIT 2
To reallocate a 2 dimension array of int, you could use something like:
int ** Realloc2D(int **arr, int cols, int rows)
{
int space = cols*rows;
int y;
arr = realloc(arr, space*sizeof(int));
for(y=0;y<cols;y++)
{
arr[y] = calloc(rows, sizeof(int));
}
return arr;
}
And here is a test function demonstrating how it works:
#include <stdio.h>
#include <stdlib.h>
int ** Create2D(int **arr, int cols, int rows);
void free2DInt(int **arr, int cols);
int ** Realloc2D(int **arr, int cols, int rows);
int main(void)
{
int **paths = {0};
int i, j;
int col = 5;
int row = 8;
paths = Create2D(paths, col, row);
for(i=0;i<5;i++)
{
for(j=0;j<8;j++)
{
paths[i][j]=i*j;
}
}
j=0;
for(i=0;i<5;i++)
{
for(j=0;j<8;j++)
{
printf("%d ", paths[i][j]);
}
printf("\n");
}
//reallocation:
col = 20;
row = 25;
paths = Realloc2D(paths, col, row);
for(i=0;i<20;i++)
{
for(j=0;j<25;j++)
{
paths[i][j]=i*j;
}
}
j=0;
for(i=0;i<20;i++)
{
for(j=0;j<25;j++)
{
printf("%d ", paths[i][j]);
}
printf("\n");
}
free2DInt(paths, col);
getchar();
return 0;
}
The realloc() does not fail. What fails is that you haven't allocated memory for the new pointers between paths[previous_npaths] and paths[new_npaths-1], before writing to these arrays in the loop for (j = 0; j < npaths; ++j).
I am trying to allocate a matrix using a function that takes its dimensions and a triple pointer. I have allocated an int** (set to NULL) and I am passing its address as the function's argument. That gives me a mem access violation for some reason.
void allocateMatrix(int ***matrix, int row, int col)
{
int i;
if((*matrix = (int**)malloc(row * sizeof(int*))) == NULL)
{
perror("There has been an error");
exit(EXIT_FAILURE);
}
for(i = 0; i < row; ++i)
{
if((*matrix[i] = (int*)malloc(col * sizeof(int))) == NULL)
{
perror("There has been an error");
exit(EXIT_FAILURE);
}
}
}
/* main.c */
int** matrix = NULL;
allocateMatrix(&matrix, MATRIX_ROW, MATRIX_COL); //error
You need to change
if((*matrix[i] = (int*)malloc(col * sizeof(int))) == NULL)
to
if(((*matrix)[i] = (int*)malloc(col * sizeof(int))) == NULL)
// ^ ^
You need to dereference matrix before using the array subscript.
*matrix[i] is equivalent to *(matrix[i])
It's a problem of operator precedence. In
if ((*matrix[i] = (int*)malloc( ... ))
the default precedence is *(matrix[i]), while you should use (*matrix)[i].
I would still recommend to allocate the matrix as a contiguous array instead as a array of pointers to arrays.
I have made a solution program for gcc C11/C99 with apropriate allocation funtions based on links:
http://c-faq.com/aryptr/dynmuldimary.html
http://c-faq.com/aryptr/ary2dfunc3.html
After some discussion in comments, it is clear that matrix2 is correctly allocated, it can be passed to this function fn(int row, int col, int array[col][row]) as matrix2[0] (data in one dimensional array) with a cast to (double (*)[])
//compile with gcc --std=c11 program.c
#include <stdio.h>
#include <stdlib.h>
#define MX 9
#define MY 14
void input_matrix(int row, int column, double matrix[row][column]);
void print_matrix(int row, int column, double matrix[row][column]);
double **alloc_matrix2(int row, int column);
double *alloc_matrix3(int row, int column);
void *alloc_matrix4(int row, int column);
int main()
{
int i=MX, j=MY;
printf("Generate input values and print matrices with functions fn(int w, int k, double matrix[w][k]) (in C99 and C11)\n");
double matrix1[i][j];
input_matrix(MX,MY,matrix1);
printf("matrix static\n");
print_matrix(MX,MY,matrix1);
double **matrix2; //data of matrix2 is just matrix3
matrix2=alloc_matrix2(MX,MY);
input_matrix(MX,MY,(double (*)[])(*matrix2));
printf("matrix two times allocated one for pointers, the second for data (double (*)[])(m[0])\n");
print_matrix(MX,MY,(double (*)[])(matrix2[0]));
free(*matrix2);
free(matrix2);
double *matrix3=alloc_matrix3(MX,MY);
input_matrix(MX,MY,(double (*)[])matrix3);
printf("matrix allocated as two-dimensional array\n");
print_matrix(MX,MY,(double (*)[])matrix3);
free(matrix3);
j=MY;
double (*matrix4)[j];
matrix4 = (double (*)[])alloc_matrix4(MX,MY);
input_matrix(MX,MY,matrix4);
printf("matrix allocated via pointer to array m = (double (*)[])malloc(MX * sizeof(*m))\n");
print_matrix(MX,MY,matrix4);
free(matrix4);
printf("\nThe End!\n");
return 0;
}
void input_matrix(int row, int column, double matrix[row][column]){
for(int i=0; i<row; i++){
for(int j=0; j<column; j++)
matrix[i][j]=i+1;
}
}
void print_matrix(int row, int column, double matrix[row][column]){
for(int i=0; i<row; i++){
for(int j=0; j<column; j++)
printf("%.2lf ", matrix[i][j]);
printf("\n");
}
}
double **alloc_matrix2(int row, int column){
double **matrix;
matrix=malloc(row*sizeof(double*));
matrix[0] = (double *)malloc(row*column*sizeof(double));
for(int i = 1; i < row; i++)
matrix[i] = matrix[0]+i*column;
return matrix;
}
double *alloc_matrix3(int row, int column){
double *matrix;
matrix=malloc(row*column*sizeof(double));
return matrix;
}
void *alloc_matrix4(int row, int column){
double (*matrix)[column];
matrix = (double (*)[])malloc(row*sizeof(*matrix));
return matrix;
}