Multidimensional Array Malloc
Hello
I am making my CPU burn hell and crash everytime I run this program, so there is some error either an infite loop or I dont know what, but it is meant to just assign on the fly from user input the number of rows, cols, and then i just fill content and print to see I did alright, but obviously, I didnt.
I d really appreciate if anybody could pinpoint where I am messing
Here it goes:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int **mat; // Pointer to pointer
int rows, cols, i, j, k;
printf("How many rows you want ");
scanf("%d", &rows);
mat = (int **)malloc(rows*sizeof(int*)); // array of number of rows
printf("How many cols ");
scanf("%d", &cols);
fflush(stdin);
for (i=0; i<rows; i++) // for each row ...
{
mat[i] = (int *)malloc (cols * sizeof(int)); // add these many cols
}
for (j = 0; j<=rows; j ++)
{
for (k = 0; k<=cols; k++)
{
mat[j][k] = j * k;
printf("so the values for the array ar %d: ", *(*(mat+j)+k));
}
}
getchar();
}