CALLOC / 2-D Array Creation
I have an assignment to create a two dimensional array based on user inputs. A prompt is done for the size of the row and column and then a 2-D array is to be created based on the inputed dimensions. The attached code compiles okay but crashes if the row and column are not the same size. For example, if I input row = 10, col= 10 then everything is okay but if I input row = 5, col = 6, then the program crashes.
Suggestions?
[code]
#include <stdio.h>
#include <stdlib.h>
#include <time.h> /* Required for random integer */
#include <windows.h> /* Required for clearScreen function */
int main (void) {
short int outer;
short int inner;
short int rowInput;
short int colInput;
short int size;
short int *row;
short int *col;
int **arrayx;
printf("Enter the row: ");
scanf("%d", &rowInput);
printf("\nEnter the col: ");
scanf("%d", &colInput);
row = &rowInput;
col = &colInput;
size = *row * *col;
printf("\n\nSize is %d", size);
printf("\n\n");
arrayx = (int **)calloc (*row, sizeof(int*));
arrayx [*col] = (int *)calloc (*col, sizeof(int));
for (outer=0; outer < *row; outer++) {
for (inner=0; inner < *col; inner++)
arrayx [*row] [*col] = 10;
/* crashes when row <> col */
/* (*(*arrayx + outer) + inner) = 1;
above statement does not work but
the one before it does seem to
compile okay */
} /* end for outer < row */
return 0;
} /* end function main */
[code]