Does anybody know why visual studio throws me an exception when i want to compare an element of the matrix with 0?
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define DIM 100
void citire(int **a, int n);
int negNumbers(int **a, int n);
void main(void)
{
int *a, n,nn;
printf("n = ");
scanf("%d", &n);
citire(&a, n);
nn = negNumbers(&a, n);
printf("\nThe negative numbers are : %d", nn);
_getch();
}
void citire(int **a, int n)
{
*a = (int *)malloc(sizeof(int)*n*n);
for (int i = 0; i < n*n; i++)
scanf("%d", *a+i);
}
int negNumbers(int **a, int n)
{
int k = 0;
*a = (int *)malloc(sizeof(int)*n*n);
for (int i = 0; i < n-1; i++)
for (int j = i; j < n; j++)
{
if (*(*(a + i) + j) < 0)
k++;
}
return k;
}
This is all the code. It throws me this exception:
0xC0000005: Access violation reading location 0xCCCCCCD0.
I have to mention that if I use the debugger the result is good.