I'm having trouble modifying a dynamic array already created. The following example depicts my problem. In this example the program compiles but gives execution error (nothing appears and shows the dialog saying that the program stopped working).
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
void Alocar(int **m,int rows,int cols){
m = (int **)malloc((rows)*sizeof(int*));
for(int i=0;i<rows;i++){
m[i] = (int *)malloc(cols*sizeof(int));
}
}
void Modificar(int **m,int rows,int cols){
for(int i=0; i<rows ;i++)
{
for(int j=0; j<cols ; j++)
{
m[i][j]= 0;
}
}
cout << " Matriz zerada" << endl;
for(int i=0; i<rows ;i++)
{
for(int j=0; j<cols ; j++)
{
cout << m[i][j]<< " ";
}
cout << endl;
}
}
int main()
{
int **M;
Alocar(M,100,100);
Modificar(M,100,100);
return 0;
}
Does anyone know why this type of problem is occurring?