I want to allocate memory for a two-dimensional array (matrix) and write the sums of the diagonals in a separate one-dimensional array. So my code has an array of pointers to pointers,
int N, ** matrix = NULL;
matrix = (int**) malloc(sizeof(int*) * N);
I fill it and then I create an array to store the sums of the diagonals,
int diag = 2 * N - 1;
int *diagonals = NULL;
diagonals = (int*)malloc(sizeof(int) * diag);
but when I want to write a value into an array, something goes wrong, the values just don't get written into the array; I don't know why.
Here is my code:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main() {
    srand(time(NULL));
    int N, ** matrix = NULL;
    printf("Input the number of rows\n");
    scanf_s("%d", &N);
    printf("\n");
    // Memory allocation for the array of pointers to pointers
    
    matrix = (int**) malloc(sizeof(int*) * N);
    if (matrix != NULL)
    {
        for (int i = 0; i < N; i++)
            *(matrix + i) = (int*)malloc(sizeof(int) * N);
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++)
            {
                matrix[i][j] = rand() % 14 - 4;
                printf("%d\t", matrix[i][j]);
            }
            printf("\n");
        }
        printf("\n");
        int diag = 2 * N - 1;
        int *diagonals = NULL;
        diagonals = (int*)malloc(sizeof(int) * diag);
            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < N; j++)
                {
                    diagonals[i+j] += matrix[i][j];;
            }
        }
            for (int i = 0; i < diag; i++) {
                printf("diagonals[%d] - %d\n",i, *(diagonals+i));
        }
    }
    else
        printf("Not enough memory.. oops..\n");
}

 
     
     
     
    