I'm learning about dynamic memory allocation in C. I searched here for the same problem and found solutions that say that it's an error for C++ compiler. What I don't understand is why am I getting this error when I'm using .c extention and not .cpp. I'm using Visual Studio Express 2012.
Here is the code I wrote if anyone needs it. It's a simple program for entering a matrix (input) and writing it on screen (output):
#include <stdio.h>
#include <stdlib.h>
void main()
{
int **a;
int i,j,n,m;
printf("Enter matrix dimensions:\n");
scanf("%d%d", &n,&m);
printf("Enter matrix:\n");
a=malloc(n*sizeof(int*));
for (i=0; i<n; i++)
{
a[i]=malloc(m*sizeof(int));
for (j=0; j<m; j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Matrix is:\n");
for (i=0; i<n; i++)
{
for (j=0; j<m; j++)
{
printf("%d ",a[i][j]);
}
putchar('\n');
}
}
Note: I did other programs (without dynamic memory allocation) in C with the same compiler and everything was fine.
Edit and update(conclusion):
I combined two answers I've got from Vlad from Moscow and Shivansh Jagga and this solved the problem. Vlads' answer helped with the compiling and Shivanshs' got rid of the error, but I have to say, as suggested in comments, that malloc doesn't need typecast and the program can work with that error, it just skippes it.
I see that answer from Shivansh Jagga was deleted for some reason. Basically his answer said that to get rid of the error you need to go to:
Project > Properties > C/C++ > General > SDL checks > No.