So I wanna create a 2D array where the number of rows and columns are taken by command line argument. Then the array is made using dynamic memory allocation where users give the input.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, int *argv[]) {
int **arr;
arr = (int **)malloc(*argv[1]*sizeof(int*));
for (int i=0; i<argv[1]; i++)
arr[i] = (int *)malloc (*argv[2]*sizeof(int));
for (int i=0; i<argv[1]; i++)
for (int j=0; i<argv[2]; j++)
scanf("%d", &arr[i][j]);
for (int i=0; i<argv[1]; i++){
for (int j=0; i<argv[2]; j++)
printf("%d ",arr[i][j]);
printf("\n");}
return 0;
}
However Iam getting segmentation dump every time Iam running this. Can you please explain where Iam doing wrong.
int main(int argc, char *argv[]). The arguments are strings, so need to be converted to int for use in the size calculation.