Im new to C coding. In order to learn the language i want to do following little "program".
I want to scan inputs , and put them into 2d array - but i do now know how many elements i want to scan / input = i want to dynamicly create 2d array. And here is the problem. I know how i can dynamicly create 1d array e.g
int MAX;
int *arr;
scanf("%d",&MAX);
arr=(int*)malloc( Max * sizeof ( int ) )
i found how to allocate 2d array e.g
int X;
int Y;
int **arr;
scanf("%d%d",&X,&Y);
arr=(int*) malloc ( X * Y * sizeof ( int* ) )
But i havent found the thing i need the most = create 2d array and increase its " memory or size " every time new item is being added. For example - what i would like to achieve.
int **arr;
int index=1;
int X;
int Y;
arr=(int *) malloc ( index * sizeof ( int ) );
while (! feof ){
scanf("%d%d",&X,&Y);
if ( index > 1 ){
index ++;
arr=realoc( arr*, index * sizeof ( arr* ) )
arr[iX][0]=X;
arr[iX][1]=Y;
} else{
arr[iX][0]=X;
arr[iX][1]=Y;
index++;
}
}
this was my attempt and i failed horribly.. how can dynamicly change size of 2d array on every input (or every iteration of loop ) i am aware of this solution but all answer are used with predefined y-osis of array. e.g arr[][25]
realloc().