Given this task:
Write a program that allocates the necessary amount of memory for storing the elements from two
[m x n]integer matrices.
I don't know how to allocate memory to 2 Dimensional arrays.
I read some examples but I don't get it.
#define _CRT_SECURE_NO_WARNINGS
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#define DIM 10
void main()
{
int **m, *n;
int i = 0, j = 0;
int diml, dimc;;
puts("Introduceti dimensiunea matricelor(linii|coloane)");
scanf("%d%d", &diml, &dimc);
m = (int**)malloc(dimc*sizeof(int));
puts("Introduceti elementele primei matrici:");
for (j = 0;j < dimc;j++)
{
m[i] = (int *)malloc(diml*(sizeof(int)));
}
for (j = 0;j < dimc;j++)
{
for (i = 0;i < diml;i++)
{
printf("tab[%d][%d]= ", j + 1, i + 1);
scanf("%*d", m[j][i]);
}
}
_getch();
}
My program crash after I enter the first line.
Introduceti dimensiunea matricelor(linhi I coloane)
3
3
Introduceti elementele primei matrici:
ab[1][1]= 2
ab[1][2]= 1
ab[1][3]= 2
ab[2][1]=
Problema 3.exe has stopped working
A problem caused the program to stop working correctly.
Windows will closethe program and notify you if a solution is
available.
m = (int**)malloc(dimc*sizeof(int));-->m = malloc(dimc*sizeof(int*));m = (int**)malloc(dimc*sizeof(int));-->m = (int**)malloc(dimc*sizeof(int*));2)m[i] = (int *)malloc(diml*(sizeof(int)));-->m[j] = (int *)malloc(diml*(sizeof(int)));3)scanf("%*d", m[j][i]);-->scanf("%d", &m[j][i]);malloc& friends in C. And there is no 2D array in your code! To alloc a 2D array, useint (*m)[DIMX_CONSTANT] = malloc(sizeof(*m) * y);