0
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void) {
    int sizestr, sizecol, **arr;
    printf("Sizestr is ");
    scanf_s("%d", &sizestr);
    printf("Sizecol is ");
    scanf_s("%d", &sizecol);
    arr = (int**)calloc(sizestr, sizeof(int*)); //creates 1d array of *int
    if (arr == NULL) {                          // checks allocation of memory
        printf("Error!\n");
        system("pause");
        return 1;
    }
    for (int i = 0; i < sizecol; i++) {
        *(arr + i) = (int*)calloc(sizecol, sizeof(int)); // creates 2nd d array of int
        if (*(arr + i) == NULL) {                        // checks every line for memory allocation
            printf("Error!\n");
            system("pause");
            return 1;
        }
    }
    for (int i = 0; i < sizestr; i++) {
        for (int j = 0; j < sizecol; j++) {
            printf("*(*(arr + %d) + %d) is ", i, j);
            scanf_s("%d", (*(arr + i) + j));            //scans every argument
        }
        printf("\n");
    }
    for (int i = 0; i < sizestr; i++) {
        for (int j = 0; j < sizecol; j++) {
            printf("%3d ", *(*(arr + i) + j));           //prints every argument
        }
        printf("\n");
    }
    system("pause");
    return 0;
}

This code creates 2d array size of which user chooses. It works with each number if strings and each number of columns, but when I set columns 1 it crashes in 2nd argument with an arror "result_pointer != nullptr".

Supy
  • 1

0 Answers0