It gives an error: "Unhandled exception at 0x010D1753 in ConsoleApplication1.exe: 0xC0000005: access violation when reading at 0x00000000".

What does it mean and how to fix it?

#include"stdafx.h" #include"conio.h" #include"locale.h" #include"stdlib.h" #include"time.h" // Функция генерации случайных чисел в диапазоне от range_min до range_max int rnd(int range_min, int range_max) { return (double)rand() / (RAND_MAX + 1) * (range_max - range_min) + range_min; } int _tmain(int argc, _TCHAR* argv[]) { setlocale(LC_ALL, "Russian"); srand((unsigned)time(NULL)); int **a; int i, min, j, sum, n, m, V, l, k; srand((unsigned)time(NULL)); printf("Введите размерность массива: \n"); scanf_s("%d", &n); a = (int**)calloc(n + 1, sizeof(int*)); for (i = 1; i <= n + 1; i++) a[i] = (int*)calloc(n + 1, sizeof(int)); for (i = 1; i <= n; i++) for (j = 1; j <= n; j++) a[i][j] = rnd (-5, 5); for (i = 1; i <= n; i++) { printf("\n"); for (j = 1; j <= n; j++) { printf(" %d", a[i][j]); } } printf("\n"); //____________________________________________________________________________________________ for (j = 0; j < n; j++) { min = 0; if (a[0][j] < a[0][j + 1] && a[0][j] < a[0][j - 1] && a[0][j] < a[1][j]) { i = 0; printf("\n %d %d", i, j); min = min + 1; } } for (j = 0; j < n; j++) { if (a[n - 1][j] < a[n - 1][j - 1] && a[n - 1][j] < a[n - 1][j + 1] && a[n - 1][j] < a[n - 2][j]) { i = n - 1; printf("\n %d %d", i, j); min = min + 1; } } //____________________________________________________________________________________________ for (i = 1; i<n - 1; i++) { for (j = 0; j < n; j++) { if (a[i][j] < a[i][j - 1] && a[i][j] < a[i][j + 1] && a[i][j] < a[i - 1][j] && a[i][j] < a[i + 1][j]) { printf("\n %d %d", i, j); min = min + 1; } } } sum = 0; for (i = 0; i<n; i++) { for (j = 1; j<n; j++) { if (i == j) { l = 10 - j; for (j = i; j < l; j++) { sum = sum + abs(a[i][j]); } } } } printf("\n Количество локальных минимумов заданной матрицы равно: %d ", min); printf("\n Сумма модулей элементов выше главной диагонали равна: %d ", sum); _getch(); } 
  • @ Red, If you are given an exhaustive answer, mark it as correct (click on the check mark next to the selected answer). - Nicolas Chabanovsky

2 answers 2

This error means that you are trying to read data from a variable pointing to NULL (or having a value of 0, which is the same thing in C).

You can fix this - if you do not read the memory from a variable pointing to NULL. Where exactly in your sheet that very place is very difficult to say, since the sheet is large. And to create a project and test it at home, because you were too lazy to localize the area when, where it happens, I'm too lazy.

    The error you have is that you then take indices from 1, then from 0.

    For example, at the very beginning:

     for (i = 1; i <= n + 1; i++) a[i] = (int*)calloc(n + 1, sizeof(int)); 

    Note a[0] will remain 0 (it is also NULL), and a little further:

     for (j = 0; j < n; j++) { min = 0; if (a[0][j] < a[0][j + 1] && a[0][j] < a[0][j - 1] && a[0][j] < a[1][j]) { 

    You refer to a[0] .

    In C, it is customary to use indices with 0.

    Therefore, correct:

     a = (int**)calloc(n + 1, sizeof(int*)); for (i = 1; i <= n + 1; i++) a[i] = (int*)calloc(n + 1, sizeof(int)); 

    on

     a = (int**)calloc(n, sizeof(int*)); for (i = 0; i < n; i++) a[i] = (int*)calloc(n, sizeof(int)); 

    etc.